LeetCode热题100- 字母异位词分组

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

看到题目想到对字符串排序不难,但是需要对结果进行去重,去重方法其实没想到,需要使用字典去重,然后将字段的values获得最终结果。

python 复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        if not strs:
            return [[]]
        
        d = {}
        for s in strs:
            char = ''.join(sorted(s))
            if char not in d:
                d[char] = []
            d[char].append(s)
        
        return list(d.values())
相关推荐
文丑颜不良啊19 小时前
LeetCode3281~3300题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode3261~3280题目解析
java·leetcode
Rain的Java大神之路1 天前
LeetCode 40 组合总和 II:面试官想听的不是回溯,而是那行去重代码
java·leetcode·面试
ashiho2 天前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
Pniubi2 天前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
文丑颜不良啊3 天前
LeetCode3041~3060题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode3101~3120题目解析
java·leetcode
玄昌盛不会编程3 天前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊3 天前
LeetCode3061~3080题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode3081~3100题目解析
java·leetcode