KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm writing an Android word app. My code includes a method that would find all combinations of the string and the substrings of a 7 letter string with a minimum of length 3. Then compare all available combination to every word in the dictionary to find all the valid words. I'm using a recursive method. Here's the code. // Gets all the permutations of a string. void permuteString(String beginningString, String endingString) { if (endingString.length() <= 1){ if((Arrays.binarySearch(mDictionary, beginningString.toLowerCase() + endingString.toLowerCase())) >= 0){ mWordSet.add(beginningString + endingString); } } else for (int i = 0; i < endingString.length(); i++) { String newString = endingString.substring(0, i) + endingString.substring(i + 1); permuteString(beginningString + endingString.charAt(i), newString); } } // Get the combinations of the sub-strings. Minimum 3 letter combinations void subStrings(String s){ String newString = ""; if(s.length() > 3){ for(int x = 0; x < s.length(); x++){ newString = removeCharAt(x, s); permuteString("", newString); subStrings(newString); } } } The above code runs fine but when I installed it on my Nexus s I realized that it runs a bit too slow. It takes a few seconds to complete. About 3 or 4 seconds which is unacceptable. Now I've played some word games on my phone and they compute all the combinations of a string instantly which makes me believe that my algorithm is not very efficient and it can be improved. Can anyone help? public class TrieNode { TrieNode a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; TrieNode[] children = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z}; private ArrayList<String> words = new ArrayList<String>();
Tags (comma-separated)
Save Edits
Cancel