Find all possible ways to Split the given string into Primes
Given a string str that represents a number. The task is to find all possible ways to split the given string such that each segment is a prime number in the range of 1 to 10^{6}.
Examples:
Input: str = “3175”
Output:
[317, 5]
[31, 7, 5]
[3, 17, 5]Explanation:
There can be 8 possible ways to split:
[3175]
[317, 5] – All primes
[31, 75]
[31, 7, 5] – All primes
[3, 175]
[3, 17, 5] – All primes
[3, 1, 75]
[3, 1, 7, 5]Input: str = “11373”
Output:
[113, 73]
[113, 7, 3]
[11, 373]
[11, 37, 3]
[11, 3, 73]
[11, 3, 7, 3]
Approach:

The idea is to generate all possible splits of a string of size N by counting binary number from 0 to 2^{(N – 1)} – 1. Where every 1 indicates that the string should split at that point.
For example:
S = "3175" 0 0 0 3175 0 0 1 317, 5 0 1 0 31, 75 0 1 1 31, 7, 5 1 0 0 3, 175 1 0 1 3, 17, 5 1 1 0 3, 1, 75 1 1 1 3, 1, 7, 5
 To check the prime number efficiently we will preprocess prime number in a boolean array using Sieve of Eratosthenes.
Below is the implementation of the above approach.
C++
// C++ program to Find all the // ways to split the given string // into Primes. #include<bits/stdc++.h> using namespace std; bool primes[1000000]; const int maxn = 1000000; // Sieve of Eratosthenes void sieve() { memset (primes, true , sizeof (primes)); primes[0] = primes[1] = 0; for ( int i = 2; i * i <= maxn; i++) { if (primes[i]) { for ( int j = i * i ; j <= maxn ; j += i) primes[j] = false ; } } } // Function Convert integer // to binary string string toBinary( int n) { string r = "" ; while (n != 0) { r = (n % 2 == 0 ? "0" : "1" ) + r; n /= 2; } return (r == "" )? "0" :r; } // Function print all the all the // ways to split the given string // into Primes. void PrimeSplit(string str) { string temp; int cnt=0; // To store all possible strings vector<string> ans; int bt = 1<<(str.size()1); int n = str.size(); // Exponetnital complexity n*(2^(n1)) // for bit for ( int i = 0 ; i < bt ; i++) { temp = toBinary(i) + "0" ; int j = 0, x = n  temp.size(), y; while (j < x) { temp = "0" + temp; j++; } j = 0; x = 0; y = 1; string sp = "" , tp = "" ; bool flag = 0; while (j < n) { sp += str[j]; if (temp[j] == '1' ) { tp += sp + ',' ; y = stoi(sp); // Pruning step if (!primes[y]) { flag = 1; break ; } sp = "" ; } j++; } tp += sp; if (sp != "" ) { y = stoi(sp); if (!primes[y]) flag = 1; } if (!flag) ans.push_back(tp); } if (ans.size() == 0) { cout << 1 << endl; } for ( auto i:ans) { cout << i << endl; } } // Driver code int main() { string str = "11373" ; sieve(); PrimeSplit(str); return 0; } 
113,73 113,7,3 11,373 11,37,3 11,3,73 11,3,7,3
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a studentfriendly price.
Recommended Posts:
 Split the given string into Primes : Digit DP
 Count of ways to split given string into two nonempty palindromes
 Ways to split string such that each partition starts with distinct character
 Ways to split array into two groups of same XOR value
 Number of ways to split N as sum of K numbers from the given range
 Prime points (Points that split a number into two primes)
 Count of ways to split a given number into prime segments
 Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
 Number of distinct ways to represent a number as sum of K unique primes
 Split the string into minimum parts such that each part is in the another string
 Length of largest subarray having primes strictly greater than nonprimes
 Number of ways to split a binary number such that every part is divisible by 2
 Find the sum of all Truncatable primes below N
 Count primes that can be expressed as sum of two consecutive primes and 1
 Print all possible ways to convert one string into another string  EditDistance
 Find the number of ordered pairs such that a * p + b * q = N, where p and q are primes
 Find the prime numbers which can written as sum of most consecutive primes
 Split the string into substrings using delimiter
 How to split a string in C/C++, Python and Java?
 Split the given string into Odds: Digit DP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.