Find anagram of input on set of strings..?

后端 未结 3 1042
小鲜肉
小鲜肉 2021-02-10 13:59

Given a set of strings (large set), and an input string, you need to find all the anagrams of the input string efficiently. What data structure will you use. And using that, how

3条回答
  •  鱼传尺愫
    2021-02-10 14:16

    Build a frequency-map from each word and compare these maps.

    Pseudo code:

    class Word
    
      string word
      map frequency
    
      Word(string w)
        word = w
        for char in word
          int count = frequency.get(char)
          if count == null
            count = 0
          count++
          frequency.put(char, count)
    
      boolean is_anagram_of(that)
        return this.frequency == that.frequency 
    

提交回复
热议问题