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())
相关推荐
Sw1zzle9 小时前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode
海石10 小时前
子树怎么找?树的3种遍历方式来帮忙!
算法·leetcode
海石10 小时前
难度分 1588:思路 + 技巧 = AC
算法·leetcode
nothing&nowhere11 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田11 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang11 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
飞猪~15 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_9563198816 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python
开飞机的舒克_17 小时前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi