(disclaimer: pseudocode may not be valid java, even though it looks like it)
It sounds like you have a jumbled collection of letters, and want to find all of the English words that can be spelled using them.
Two strings are anagrams of one another if they compare equal when you sort both of them. Permuting the order of letters in your candidate word to see if any of them are legal English words is expensive. Instead, sort the letters and compare it against your word list:
boolean is_anagram(string word_a, string word_b){
return sorted(word_a).equals(sorted(word_b));
}
List<string> valid_anagrams(string candidate_word){
anagrams = new List<string>();
foreach(string word : list_of_words){
if (is_anagram(candidate, word)){
anagrams.push(word);
}
}
return anagrams;
}
This is more efficient if the number of words in your word list is less than the factorial of the size of your candidate word. For instance, the number of legal words in Words With Friends is about 170,000, so you would prefer the above method for checking words of length 9 or more.
If you plan to check a lot of candidate words, then you can save time by saving the sorted forms of all of your valid words. Create a dictionary where the key is a sorted string, and the value is a list of English words that are an anagram of that string. It should look like this:
{
"act": ["act", "cat", "tab"],
"abll": ["ball"],
"aeprs": ["asper", "parse", "pears", "reaps", "spare", "spear"]
}
You can construct this dictionary once at the beginning of your program, like so:
d = new Dictionary<string, List<string>>();
foreach (string word in list_of_words){
string key = sorted(word)
if (!d.contains_key(key)){
d[key] = new List<string>();
}
d[key].push(word);
}
then finding valid anagrams for a string is just a matter of accessing the dictionary.
List<string> valid_anagrams(string candidate_word){
string key = sorted(candidate_word);
if (!d.contains_key(key)){
return new List<string>();
}
else{
return d[key];
}
}