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())
相关推荐
心静财富之门8 分钟前
Python 数据共享四种方案完整实例(同进程 / JSON / Redis / HTTP)
数据库·python·oracle
王仔小奶38 分钟前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
用户03321266636742 分钟前
使用 Python 快速在 PowerPoint 中生成表格【教程】
python
夜晚回家1 小时前
Python 身份运算符详解 | is 与 == 到底差在哪
python
fengye2071611 小时前
cpython框架结构
python
用户91294046848421 小时前
大模型返回 JSON 后,先过这道 Python 校验
python
Dmz丶1 小时前
前端转 AI 100 天 Day 32:LLM 客户端工程化——重试、超时、降级、限流
python
曲径海盗2 小时前
【Python 学习第 39 天】for 循环
python·学习笔记·编程入门
万联WANFLOW2 小时前
Kimi K3.1 Pro 接口接入指南:模型选型、费用计算与零数据保留
java·spring boot·python·安全·spring·大模型
INGNIGHT2 小时前
440 · 背包问题(三)(无限次数)
算法·leetcode·动态规划