【LeetCode】字母异位词分组

目录


一、题目

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

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。

示例 1:

输入: strs = "eat", "tea", "tan", "ate", "nat", "bat"

输出: \["bat","nat","tan","ate","eat","tea"]

示例 2:

输入: strs = ""

输出: \[""]

示例 3:

输入: strs = "a"

输出: \["a"]

提示:

1 <= strs.length <= 104

0 <= strsi.length <= 100

strsi 仅包含小写字母


二、解法

使用哈希表

可以将每个单词,排一下序,然后相同排序的单词放到一起,最后,将每个list输出就好啦


完整代码

python 复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        dic = collections.defaultdict(list)
        for s in strs:
            dic[''.join(sorted(s))].append(s)
        return list(dic.values())

相关推荐
Nil20836 分钟前
leetcode 48旋转图像
算法·leetcode·职场和发展
Nil2081 小时前
leetcode 206反转链表
算法·leetcode·链表
想吃火锅100514 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20815 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
evans在进步20 小时前
LeetCode 394:字符串解码——Java 单栈模拟与嵌套解析详解
java·python·leetcode
Nil20820 小时前
leetcode 189轮转数组
数据结构·算法·leetcode
吃着火锅x唱着歌21 小时前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王21 小时前
LeetCode hto100——字母异位词分组
java·算法·leetcode
土司大王1 天前
LeetCode hot100——两数之和
数据结构·算法·leetcode
.道阻且长.1 天前
7.LeetCode算法习题讲解--双指针--四数之和
算法·leetcode·职场和发展