LeetCode 热题 100第二题:字母易位词分组python版本

第一部分:题目信息

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

示例 1:

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]

输出:[["bat"],["nat","tan"],["ate","eat","tea"]]

解释:

  • 在 strs 中没有字符串可以通过重新排列来形成 "bat"
  • 字符串 "nat""tan" 是字母异位词,因为它们可以重新排列以形成彼此。
  • 字符串 "ate""eat""tea" 是字母异位词,因为它们可以重新排列以形成彼此。

示例 2:

输入: strs = [""]

输出:[[""]]

示例 3:

输入: strs = ["a"]

输出:[["a"]]

提示:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] 仅包含小写字母

第二部分:代码实现

讲解如下:

第一步:对原始列表中的每个字符串进行字母排序,生成排序后字符串列表

核心目的 :为每个原始字符串生成一个 "特征标识"(字母排序后的字符串),后续通过这个特征标识判断哪些原始字符串是同类(比如eattea排序后都是aet,说明它们是同类)。

复制代码
#第一步:重新排序
str_sort=[]
for in_str in strs:
    first = in_str
    list_first = list(first)
    list_first_sort = sorted(list_first)
    first_sort = ''.join(list_first_sort)
    str_sort.append(first_sort)
print(str_sort)

第二步:通过集合去重,获取同类字符串的类别数量(count)

核心目的 :统计有多少组不同的字符串(即最终二维列表有多少个内层列表),set 集合的 "元素唯一性" 可自动剔除 str_sort 中的重复特征,长度即为类别数。

复制代码
#第二步:得到相同元素的类别数量count(set集合自动去重)
str_sort_set=set(str_sort)
count=len(str_sort_set)

第三步:创建一个包含 count 个空列表的二维列表(str_count)

核心目的 :提前搭建归类的 "容器",准备好 count 个空的内层列表,后续将同类的原始字符串存入对应的空列表中。

复制代码
#第三步:得到count个归类的数组,双层list
str_count=[]
for i in range(count):
    str_count.append([])

第四步:按 "排序特征" 匹配,将原始字符串归类到对应内层列表中

核心目的:通过 "排序特征" 的匹配,把原始字符串存入对应的内层列表,完成最终归类,这是代码的核心业务逻辑。

复制代码
#第四步:开始存放元素到指定位置
count_index=0
for target in str_sort_set:
    for j in range(len(str_sort)):
        if(str_sort[j]==target):
            str_count[count_index].append(strs[j])
    count_index=count_index+1
print(str_count)

完整代码:

复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        #第一步:重新排序
        str_sort=[]
        for in_str in strs:
            first = in_str
            list_first = list(first)
            list_first_sort = sorted(list_first)
            first_sort = ''.join(list_first_sort)
            str_sort.append(first_sort)
        print(str_sort)
        #第二步:得到相同元素的类别数量count(set集合自动去重)
        str_sort_set=set(str_sort)
        count=len(str_sort_set)

        #第三步:得到count个归类的数组,双层list
        str_count=[]
        for i in range(count):
            str_count.append([])
        #第四步:开始存放元素到指定位置
        count_index=0
        for target in str_sort_set:
            for j in range(len(str_sort)):
                if(str_sort[j]==target):
                    str_count[count_index].append(strs[j])
            count_index=count_index+1
        return str_count
相关推荐
孟健12 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞14 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽16 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
chlk12320 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑20 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件21 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
敏编程21 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪21 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook21 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移