I'm trying to write a method that returns a sub lists and each containin the max value of strigns from a larger list.
I'm using iterator and backtrack recursion to achieve this, however - I'm not familiar with backtrack recursion. Any suggestions on how to make this work. Or if my code needs adjustments?
This is what I have so far:
private void printAnagrams(List<String> anagrams, int max, List<List<String>> listofLists) {
Iterator<String> iterate = anagrams.iterator();
String word = "";
while (iterate.hasNext()) {
for(int i = 0; i < anagrams.size(); i++) {
word = iterate.next();
listofLists.add(new ArrayList<>());
listofLists.get(i).add(word);
if (listofLists.size() == max) {
listofLists.add(new ArrayList<>());
// Continue new list
}
}
}
}
This is the method that calls the private method:
public void printAnagrams(String phrase, int max, List<List<String>> anagrams) {
if (anagrams == null || max < 0) {
throw new IllegalArgumentException();
} else if (max == 0) {
getWords(phrase);
} else
printAnagrams(phrase, max, anagrams);
}// End of printAnagram method
Sample output:
List:
[core, course, cs, cure, for, force, forces, four, of, off, offer, offers, or, our, ours, re, score, so, source, suffer, sure, us, use, user]
SubLists with max = 3:
[core, off, us]
[core, us, off]
[course, off]
[cure, off, so]
[cure, so, off]
[force, of, us]
[force, us, of]