Python group the same string by the sorted(string) and store in dictionary


  • 0
    class Solution(object):
        def groupAnagrams(self, strs):
            ans, dic = [], collections.defaultdict(list)
            for s in strs:
                dic[''.join(sorted(s))].append(s)
            for d in dic:
                ans.append(dic[d])
            return ans
    

Log in to reply
 

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