I would like to know, how could I split a string in all possible way?
For example: 123456789
It's easy to write the code, to split into two.
1 23456789
12 3456789
123 456789
1234 56789
12345 6789
123456 789
1234567 89
12345678 9
And it's also easy to write a code to split into 3.
But, I can't figour it out, how could I split in all possible way? (into 1,2,3,...n)
The problem is, that I don't know, how long is the input.
Each split of string with length n can be represented by n-1 binary digits where nth digit is denoting if there should be split between nth and n+1th string symbol.
number: 0 0 1 0 0 0 0 1
string: 1 2 3|4 5 6 7 8|9
As you can see from example number 00100001b is denoting split 123 45678 9.
As there 2n-1 possible binaly numbers with lenght of n-1, there exactly that number of splits.
So you just need to create function which will give you a split based on number, generate all possible binary numbers and call that function with each number.
Recursion is a good idea, but I would like to store them in a 2 dimensional vector.
If the the lenght of the input is n than I will have max. n pieces. And the number of all combination is 2^(n-1).
So I need to store them in a nĂ—2^(n-1) matrix. But n isn't a constant, so I have to use vectors.
I would like to use the substr() function to split the string.
set the beginning character to 0.
iterate over "split" array. As soon as you encounter 1, remember its position n. If no 1 is found, set n to size of split array;
set the end character to n;
extract substring [0,n];
if we still not reach the end of split array:
set beginning character to end character + 1;
continue iterating through split array (go to 2nd line)