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())
相关推荐
放荡不羁的野指针18 分钟前
leetcode150题-字符串
数据结构·算法·leetcode
橘颂TA32 分钟前
【剑斩OFFER】算法的暴力美学——存在重复元素Ⅱ
算法·leetcode·哈希算法·散列表·结构与算法
cg501743 分钟前
力扣数据库——组合两个表
sql·算法·leetcode
AI Echoes1 小时前
LangChain 非分割类型的文档转换器使用技巧
人工智能·python·langchain·prompt·agent
2501_941798731 小时前
面向微服务分布式事务补偿与最终一致性的互联网系统高可用设计与多语言工程实践分享
leetcode·模拟退火算法
程序之巅1 小时前
VS code 远程python代码debug
android·java·python
__如风__2 小时前
onlyoffice文档转换服务离线部署
python
今晚务必早点睡2 小时前
写一个Python接口:发送支付成功短信
开发语言·python
ada7_2 小时前
LeetCode(python)22.括号生成
开发语言·数据结构·python·算法·leetcode·职场和发展
2501_941871452 小时前
面向微服务链路追踪与全局上下文管理的互联网系统可观测性设计与多语言工程实践分享
大数据·数据库·python