This is an interview question:
Given a string, find all its permutations, which are a word in the dictionary.
My decision:
Put all the dictionary words in the suffix tree and then search for each line permutation in the tree.
Search time O(n), where nis the size of the string. But the string may have permutations n!.
O(n)
n
n!
How to increase efficiency?
Your overall approach is not bad.
, , , .
, , , . , . aelp.
plea . , , ( ) :
... aelp -> pale aelp -> plea ...
, , aelp (, , -, ), 4!= 24 .
- .
, , . - .
.
, :
Array (him, hip, his, hit, hob, hoc, hod, hoe, hog, hon, hop, hos, hot)
:
Array (him, hip, his, hit, bho, cho, dho, eho, gho, hno, hop, hos, hot)
Array (bho, cho, dho, eho, gho, him, hip, his, hit, hno, hop, hos, hot)
, , - .
- ? , O (1) . , , , , . :
result_list = empty; for(char in input) { if(char not in letter_table) { return result_list; } } for(entry in permutations of input) { if(entry in dictionary_hash_table) { result_list->add_entry(); } } return result_list
. . , trie.
http://en.wikipedia.org/wiki/Trie
,
1) "next_permutation", .
2) Use "find / find_if" to find it with a dictionary.