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())
相关推荐
jayson.h16 分钟前
python-可视化提取表格-通用
开发语言·python
橘子汽水16842 分钟前
Leetcode 198,118打家劫舍,杨辉三角
算法·leetcode
悟天特斯1 小时前
AI驱动的楼宇节能:从粗放管控到精准降碳的实践路径
开发语言·人工智能·python·物联网
名字还没想好☜1 小时前
Python 用 tempfile 安全创建临时文件:NamedTemporaryFile、TemporaryDirectory 与别自己拼 /tmp 的坑
开发语言·后端·python·安全·编程语言
北城bot2 小时前
把 Python 脚本打包成 exe
python
萧鼎2 小时前
Python 数据验证神器 Pydantic:数据解析与验证、设置管理、JSONSche、与ORM集成全搞定
开发语言·python
529宝宝起名网2 小时前
用 Python 开发名字字源查询工具:从甲骨文到楷书的字形演变与文化寓意解析
开发语言·python
2601_962300473 小时前
学习Python 爬虫路线
爬虫·python·学习路线·数据解析·反爬虫
隐擎fox3 小时前
跨越传输层防线:深入 TCP/IP 协议栈指纹(p0f)原理与 Python 原始套接字检测实战
python·网络协议·tcp/ip·网络安全·dns
午彦琳3 小时前
2026.9.11
数据结构·python·算法