【力扣49. 字母异位词分组】哈希表(python3)

题目描述

https://leetcode-cn.com/problems/group-anagrams/

思路题解

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        m=dict()
        ans=[[]]
        for s in strs:
            t="".join(sorted(s))
            if t in m:ans[m[t]].append(s)
            else:
                m[t]=len(m)
                if m[t]>0:ans.append([])
                ans[m[t]].append(s)            
        return ans

【力扣49. 字母异位词分组】哈希表(python3)_第1张图片

你可能感兴趣的:(#,基本算法,python,字符串,leetcode)