Given a numeric string str, the task is to count the number of ways the given string can be split, such that each segment is a prime number. Since the answer can be large, return the answer modulo 109 + 7.
Note: A split that contains numbers with leading zeroes will be invalid and the initial string does not contain leading zeroes.
Input: str = “3175”
There are 3 ways to split this string into prime numbers which are (31, 7, 5), (3, 17, 5), (317, 5).
Input: str = “11373”
There are 6 ways to split this string into prime numbers which are (11, 3, 7, 3), (113, 7, 3), (11, 37, 3), (11, 3, 73), (113, 73) and (11, 373).
Naive Approach: To solve the problem mentioned above, the naive method is to use Recursion.
- Start recursing from ending index of the given string and consider every suffix up to 6 digits (given that the prime number must be in the range of [1, 106)] and check if it is a prime number or not.
- If the suffix doesn’t contain a leading zero and it is a prime number, then recursively call the function to count the ways for the remaining string and add to the total count.
- When the index reaches 0, we reach the base case and return 1 to consider current splits as a valid count.
- Take mod of the count at each iteration and return the count at the end.
Below is the implementation above approach:
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The optimize the above method the main idea is to use memoization technique to reduce the time complexity of the recursion solution discussed above. Let us consider a dp table which stores at every index dp[i], the ways to split the first i digits of the string str. The complexity to check if a number is prime or not can be further reduced by using Sieve of Eratosthenes.
Below is the implementation of the above approach:
Time Complexity: O(N + N*log(log(N)))
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of ways to represent N as sum of a prime number and twice of a square
- Split the array into odd number of segments of odd lengths
- Minimum splits required to convert a number into prime segments
- Number of ways to split a binary number such that every part is divisible by 2
- Number of ways to split N as sum of K numbers from the given range
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Split the number N by maximizing the count of subparts divisible by K
- Prime points (Points that split a number into two primes)
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count number of ways to cover a distance | Set 2
- Count number of ways to arrange first N numbers
- Count ways to express even number ‘n’ as sum of even integers
- Count ways to express a number as sum of powers
- Count the number of ways to traverse a Matrix
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Count number of ways to jump to reach end
- Count ways to express a number as sum of exactly two numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.