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
相关推荐
桑榆4164 小时前
双系统(Windows + Ubuntu)安装流程
linux·windows·ubuntu
从零开始学习人工智能8 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
卷无止境10 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
ltl10 小时前
实时 OS 巡礼:VxWorks、QNX、Zephyr 与 PREEMPT_RT
linux
zhanghaha131410 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
我是谁??10 小时前
Ubuntu22.04更换清华源
linux·运维·服务器
Python私教10 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教11 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
卷无止境11 小时前
FastAPI 的Admin面板生态
后端·python
Shell运维手记11 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github