Count the ways to split a digit string into primes below one million

Quick Overview

Count how many ways a string of up to 1,000 digits can be cut into consecutive parts that are each a prime below one million with no leading zeros, modulo 1,000,000,007. It tests prefix dynamic programming, sieve precomputation and careful handling of zeros inside the string.

Count the ways to split a digit string into primes below one million

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s` consisting of digits, count the number of ways to split it into substrings such that each substring represents a prime number smaller than `10^6`. A substring must not have leading zeros ("03" is not a valid representation of 3). Each character in the string must belong to exactly one substring in the split. Return the total number of valid splits modulo `10^9 + 7`. ### Function Signature ```python def count_prime_splits(s: str) -> int: ``` ### Rules - A split cuts `s` into consecutive, non-empty parts that keep their order. Two splits are different when their cut positions differ. - Each part, read as a decimal integer, must be prime and smaller than `10^6`, so a part has at most six digits. - A part may not start with `'0'`. This also rules out the part `"0"`, which is not prime anyway. - Return 0 when no valid split exists. ### Constraints - `1 <= s.length <= 1000` - `s` consists of digits `'0'` through `'9'` only. ### Examples ```text Input: s = "3175" Output: 3 ``` The valid splits are `["3", "17", "5"]`, `["31", "7", "5"]` and `["317", "5"]`. Every split has to end with the part `"5"`, because 75, 175 and 3175 are multiples of 5. ```text Input: s = "11373" Output: 6 ``` The valid splits are `["11", "373"]`, `["11", "37", "3"]`, `["11", "3", "73"]`, `["11", "3", "7", "3"]`, `["113", "73"]` and `["113", "7", "3"]`. The first part must be 11 or 113, because 1 is not prime and 1137 and 11373 are multiples of 3. ```text Input: s = "1000003" Output: 0 ``` Any first part shorter than the whole string is 1, 10, 100, 1000, 10000 or 100000, and none of those is prime. The whole string is the prime 1000003, but it has seven digits, so it is not an allowed part.

Overview: Count how many ways a string of up to 1,000 digits can be cut into consecutive parts that are each a prime below one million with no leading zeros, modulo 1,000,000,007. It tests prefix dynamic programming, sieve precomputation and careful handling of zeros inside the string.

Snowflake logo
Snowflake
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting of digits, count the number of ways to split it into substrings such that each substring represents a prime number smaller than 10^6.

A substring must not have leading zeros ("03" is not a valid representation of 3). Each character in the string must belong to exactly one substring in the split.

Return the total number of valid splits modulo 10^9 + 7.

Function Signature

def count_prime_splits(s: str) -> int:

Rules

  • A split cuts s into consecutive, non-empty parts that keep their order. Two splits are different when their cut positions differ.
  • Each part, read as a decimal integer, must be prime and smaller than 10^6 , so a part has at most six digits.
  • A part may not start with '0' . This also rules out the part "0" , which is not prime anyway.
  • Return 0 when no valid split exists.

Constraints

  • 1 <= s.length <= 1000
  • s consists of digits '0' through '9' only.

Examples

Input: s = "3175"
Output: 3

The valid splits are ["3", "17", "5"], ["31", "7", "5"] and ["317", "5"]. Every split has to end with the part "5", because 75, 175 and 3175 are multiples of 5.

Input: s = "11373"
Output: 6

The valid splits are ["11", "373"], ["11", "37", "3"], ["11", "3", "73"], ["11", "3", "7", "3"], ["113", "73"] and ["113", "7", "3"]. The first part must be 11 or 113, because 1 is not prime and 1137 and 11373 are multiples of 3.

Input: s = "1000003"
Output: 0

Any first part shorter than the whole string is 1, 10, 100, 1000, 10000 or 100000, and none of those is prime. The whole string is the prime 1000003, but it has seven digits, so it is not an allowed part.