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())
相关推荐
ZTLJQ4 小时前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
H5css�海秀4 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
阿贵---4 小时前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
无敌昊哥战神4 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
x_xbx5 小时前
LeetCode:148. 排序链表
算法·leetcode·链表
李昊哲小课6 小时前
第1章-PySide6 基础认知与环境配置
python·pyqt·pyside
2401_894241926 小时前
用Pygame开发你的第一个小游戏
jvm·数据库·python
木井巳7 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先
lightqjx7 小时前
【算法】二分算法
c++·算法·leetcode·二分算法·二分模板
Zzzz_my7 小时前
正则表达式(RE)
pytorch·python·正则表达式