I'm running into a problem while using a Trie in Java for an anagram solver. The following code is for Android and does not give me any permutation results for the letters that are given and passed in. However, if I implement the same code in a Java desktop application, it returns the correct results for me. I'm pretty sure it's something simple that's going wrong, I just can't seem to find it.
Code for Desktop version:
static String myWord;
static String myLetters = "";
static char[] myChars;
static TrieNode myNode = new TrieNode();
static TrieNode currentNode;
static ArrayList<String> availableWords = new ArrayList<String>();
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
myLetters = s.next();
readWords();
getPermutations();
}
public static void getPermutations(){
currentNode = myNode;
for(int x = 0; x < myLetters.length(); x++){
if(currentNode.children[myLetters.charAt(x) - 'a'] != null){
availableWords.addAll(currentNode.getWords());
currentNode = currentNode.children[myLetters.charAt(x) - 'a'];
System.out.println("x: " + x + " " + currentNode.getWords() + "" + myLetters.charAt(x));
}
}
System.out.println(availableWords);
}
Desktop Output:
aelpp //(user input from scanner)
x: 0 []a
x: 1 [ae]e
x: 2 [ale, lea]l
x: 3 [leap, pale, peal, plea]p
x: 4 [appel, apple, pepla]p
[ae, ale, lea, leap, pale, peal, plea]
Android code:
static String myWord;
static char[] myChars;
static TrieNode myNode = new TrieNode();
static TrieNode currentNode;
static ArrayList<String> availableWords = new ArrayList<String>();
public static void getPermutations(String passedInLetters){
currentNode = myNode;
Log.i("TRIE:" , passedInLetters);
for(int x = 0; x < passedInLetters.length(); x++){
if(currentNode.children[passedInLetters.charAt(x) - 'a'] != null){
availableWords.addAll(currentNode.getWords());
Log.i("TRIE:" , "x: " + x + " " + currentNode.getWords());
currentNode = currentNode.children[passedInLetters.charAt(x) - 'a'];
}
}
Log.i("TRIE:", availableWords.toString());
}
Android Log Output:
03-15 22:45:11.670: I/TRIE:(4286): aelpp
03-15 22:45:11.670: I/TRIE:(4286): x: 0 []
03-15 22:45:11.670: I/TRIE:(4286): x: 1 []
03-15 22:45:11.670: I/TRIE:(4286): x: 2 []
03-15 22:45:11.670: I/TRIE:(4286): x: 3 []
03-15 22:45:11.670: I/TRIE:(4286): x: 4 []
03-15 22:45:11.670: I/TRIE:(4286): []
If needed, here are the full classes:
Desktop: http://pastie.org/3604515 Android: http://pastie.org/3604513
I have been studying this post among some of my other sources: java string permutations and combinations lookup
Thanks!
EDIT:
These lines needed switched inside the if statement:
currentNode = currentNode.children[passedInLetters.charAt(x) - 'a'];
availableWords.addAll(currentNode.getWords());