Python | Leetcode Python题解之第49题字母异位词分组

题目:

题解:

python 复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        mp = collections.defaultdict(list)

        for st in strs:
            counts = [0] * 26
            for ch in st:
                counts[ord(ch) - ord("a")] += 1
            # 需要将 list 转换成 tuple 才能进行哈希
            mp[tuple(counts)].append(st)
        
        return list(mp.values())
相关推荐
一点一木5 分钟前
Seed Evolving 深度测评:我用它造了一个 AI 出题工具
人工智能·python·github
hold?fish:palm1 小时前
20 旋转图像
c++·算法·leetcode
阿童木写作1 小时前
跨境卖家效率翻倍,跨马翻译批量图片翻译工具实测
人工智能·python
鹿鹿学长1 小时前
2026国赛报名季:从组委会第一次通知到各校8月报名通知,赛程报名评奖官方口径一次讲清
python·自动化
C++、Java和Python的菜鸟1 小时前
第15章 项目部署(Docker)
python
风之清扬1 小时前
AI agent学习之一基础知识
人工智能·python·学习
Jay-r1 小时前
极简博客搭建(精简版):textlog + GitHub Pages
python·github·教程·集成学习·博客网站·技术教程·自我成长
Albart5751 小时前
【已解决】IndexError_ list index out of range 列表索引越界 全套根治方案
python·indexerror·列表索引越界·python 报错解决·python 排错
卷心菜的学习路2 小时前
基于多模态向量检索的数学相似题推荐系统:完整设计、算法与实验
java·python·算法·大模型·推荐算法·数学相似题
小南知更鸟2 小时前
【leetcodehot100】leetcode-hot100-20260809更新
算法·leetcode·职场和发展