Here was my algorithm:
Function(string, rest):
For each letter in the input
append the letter to string
add the string to a hashset
Function(string + new appended letter, rest - appended letter)
remove the appendation
//Adds every combination of the letters to a hashset private void getWords(StringBuilder string, StringBuilder append) { //Base Case if(append.toString().isEmpty()) { return; } //Recursive case //For each letter in append, append to string then recurse with string and append //without the appended letter for(int i = 0; i < append.length(); i++) { string.append(append.charAt(i)); set.add(string.toString()); getWords(string, new StringBuilder(append).deleteCharAt(i)); string.deleteCharAt(string.length()-1); } }
No comments:
Post a Comment