We define an alphabet containing every letter that we can have in our list of words. Next, for each of the letters of the alphabet, we need a different prime number, I recommend using the smallest that you can find.
This will give us the following mapping: {a => 2, b => 3, c => 5, d => 7, etc.}
Now count the letters in the word that you want to represent as an integer, and build the integer of the result as follows:
pseudo code:
result = 1 for each letter: ....result *= power(prime[letter], count(letter,word)
a few examples:
aaaa => 2 ^ 4
aabb => 2 ^ 2 * 3 ^ 2 = bbaa = baba = ...
etc.
That way, you will have an integer representing each word in your dictionary, and the word you want to test can be converted to an integer. Therefore, if n is the size of your vocabulary list, and k is the size of the longest word, then to create a new dictionary and O (k) you need O (nk) to check the new word.
Hackthissite.com has a programming problem, which is this: given the scrambled word, look in the dictionary to see if there are any anagrams in it. There is an effective solution to the problem with which I took the answer, and further optimization is also discussed in detail.
source share