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())
相关推荐
玛丽莲茼蒿2 天前
Leetcode hot100 每日温度【中等】
算法·leetcode·职场和发展
样例过了就是过了2 天前
LeetCode热题100 分割等和子集
数据结构·c++·算法·leetcode·动态规划
北顾笙9802 天前
day38-数据结构力扣
数据结构·算法·leetcode
m0_629494732 天前
LeetCode 热题 100-----14.合并区间
数据结构·算法·leetcode
xin_nai2 天前
LeetCode热题100(Java)(5)普通数组
算法·leetcode·职场和发展
水蓝烟雨2 天前
3337. 字符串转换后的长度 II
算法·leetcode
_日拱一卒2 天前
LeetCode:226翻转二叉树
数据结构·算法·leetcode
踩坑记录2 天前
leetcode hot100 64. 最小路径和 medium 递归优化
leetcode·深度优先
样例过了就是过了2 天前
LeetCode热题100 最长有效括号
c++·算法·leetcode·动态规划
水蓝烟雨2 天前
3335. 字符串转换后的长度 I
算法·leetcode