One method for determining whether a character set is an anagram of a word involves primes. Assign a prime number to each letter, for example a = 2, b = 3, c = 5, d = 7. Now precommute the product of primes for each word in the dictionary. For example, add = 2 * 7 * 7 = 98 or bad = 3 * 2 * 7 = 42.
Now, determining whether a set of letters is an anagram of any word in the dictionary can be done by calculating the value of the set of letters. For example, the letters "abd" = 2 * 3 * 7 = 42 = 'bad'. Just check if the computed value for the letters exists in your precomputed dictionary. For any anagram, you only need to do this calculation once against trying to generate all possible anagrams. Please note, however, this method will only work for relatively small words, otherwise you will run into overflow problems and will have to use BigInteger.
source share