【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())

相关推荐
Persistent的粽子!4 小时前
双指针算法:最大盛水容器
c++·算法·leetcode
All for pursuit.11 小时前
【二叉树-10】114.二叉树展开为链表
数据结构·c++·算法·leetcode·链表
旖旎夜光6 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
CoderYanger7 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
圣保罗的大教堂7 天前
leetcode 3524. 求出数组的 X 值 I 中等
leetcode
Tim_107 天前
【LeetCode】338、比特位计数
c++·算法·leetcode
mmmmath_37 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Navigator_Z7 天前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.7 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
开开心心就好7 天前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法