1. 程式人生 > >python leetcode 49. Group Anagrams

python leetcode 49. Group Anagrams

對每個字串進行排序,那麼排序後Anagrams的字串是相同的

class Solution:
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        dict1={}
        for i in range(len(strs)):
            strsort = ''.join(sorted(strs[i]))
            if not strsort in
dict1: dict1[strsort]=[i] else: dict1[strsort].append(i) res=[] for list1 in dict1.values(): temp=[] for index in list1: temp.append(strs[index]) res.append(temp) return res