I\'m trying to create a data structure that holds all the possible substring combinations that add up to the original string. For example, if the string is \"java\"
A different recursive solution that just adds to the list results
static List> substrings(String input) {
List> result = new ArrayList<>();
if (input.length() == 1) {
result.add(Arrays.asList(new String[]{input}));
}
else {
//iterate j, ja, jav, jav
for (int i = 0; i < input.length()-1; i++ ) {
String root = input.substring(0,i+1);
String leaf = input.substring(i+1);
for( List strings: substrings(leaf) ) {
ArrayList current = new ArrayList();
current.add(root);
current.addAll(strings);
result.add(current);
}
}
//adds the whole string as one of the leaves (ie. java, ava, va, a)
result.add(Arrays.asList(new String[]{input}));
}
return result;
}