I am studying practice algorithm questions and can not seem to solve this question. I am giving a number such as 1234
. I need to find the different combinations of the number when adding a +
symbol into it. There can be multiple plus symbols in the answer. So results would look like 1+234
,1+2+34
,12+34
etc. I know how to find the different substrings but not how to add them up. Here is what I have:
public static void findCombos(String string){
List<String> substrings = new ArrayList<>();
for( int i = 0 ; i < string.length() ; i++ )
{
for( int j = 1 ; j <= string.length() - i ; j++ )
{
String sub = string.substring(i, i+j);
substrings.add(sub);
}
}
System.out.println(substrings);
}
This just stores the different substrings if convert the number to a string. How would I put these together to create the correct string. I was thinking recursion with prefixes but could not get that right.
This is different than the permutation question because I am asked to keep the number in the same order but add +
's to it.