Simple AC python code using more functional style


  • 0
    Y
    def anagrams(self, strs):
      group = {}
      def getHist(word):
        hist = "".join(map(lambda x: str(word.count(x)), string.ascii_letters))
        group[hist] = group.get(hist, []) + [word.strip()]
      map(getHist, strs)
      mapping = map(lambda x: group[x], filter(lambda x: len(group[x]) > 1, group))
      if len(mapping) < 1:
          return mapping
      return reduce(lambda x, y: x + y, mapping)
    

    Idea is simple: map all words to their corresponding letter occurrence histograms. Then reduce the result as a single list. Trying to use as few mutable vars as possible.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.