Given an array of stringsĀ strs, groupĀ the anagramsĀ together. You can return the answer inĀ any order.

AnĀ AnagramĀ is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

solution

class Solution:
  def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
    d = collections.defaultdict(list) 
    for s in strs:
      key = str(sorted(s))
      d[key].append(s) 
    return d.values()
  • use a hashmap and group the strings by sorted values.
  • just return the groupings at the end.