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())
相关推荐
IT知识分享11 分钟前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
lunzi_082616 分钟前
【学习笔记】《Python编程 从入门到实践》第8章:函数定义、参数传递与模块导入
笔记·python·学习
杨运交33 分钟前
[030][Web模块]Spring Boot 验证与 OpenAPI 集成实战:从校验规则到文档生成
前端·spring boot·python
培培说证1 小时前
2026财务岗位如何快速提升自身能力
python
努力攻坚操作系统1 小时前
编程语言编译运行机制对比:C / Java / Python
java·c语言·python
godspeed_lucip1 小时前
LLM和Agent——专题6:Multi Agent 入门(5)
人工智能·python
小欣加油1 小时前
leetcode287寻找重复数
数据结构·c++·算法·leetcode
Metaphor6922 小时前
使用 Python 给 PDF 设置背景色或背景图
数据库·python·pdf
郝亚军2 小时前
如何让pycharm-2026.1.2顶部菜单栏固定显示在最上端
python