Finding anagrams for a given word

后端 未结 12 1060
温柔的废话
温柔的废话 2020-12-04 07:43

Two words are anagrams if one of them has exactly same characters as that of the another word.

Example : Anagram & Nagaram are anagrams

12条回答
  •  醉酒成梦
    2020-12-04 08:02

    We know that if two words don't have the same length, they are not anagrams. So you can partition your dictionary in groups of words of the same length.

    Now we focus on only one of these groups and basically all words have exactly the same length in this smaller universe.

    If each letter position is a dimension, and the value in that dimension is based on the letter (say the ASCII code). Then you can calculate the length of the word vector.

    For example, say 'A'=65, 'B'=66, then length("AB") = sqrt(65*65 + 66*66). Obviously, length("AB") = length("BA").

    Clearly, if two word are anagrams, then their vectors have the same length. The next question is, if two word (of same number of letters) vectors have the same length, are they anagrams? Intuitively, I'd say no, since all vectors with that length forms a sphere, there are many. Not sure, since we're in the integer space in this case, how many there are actually.

    But at the very least it allows you to partition your dictionary even further. For each word in your dictionary, calculate the vector's distance: for(each letter c) { distance += c*c }; distance = sqrt(distance);

    Then create a map for all words of length n, and key it with the distance and the value is a list of words of length n that yield that particular distance.

    You'll create a map for each distance.

    Then your lookup becomes the following algorithm:

    1. Use the correct dictionary map based on the length of the word
    2. Compute the length of your word's vector
    3. Lookup the list of words that match that length
    4. Go through the list and pick the anagrams using a naive algorithm is now the list of candidates is greatly reduced

提交回复
热议问题