49. 字母异位词分组 (Python3)

Problem: 49. 字母异位词分组

思路

参考:

  • https://leetcode.cn/problems/group-anagrams/solutions/2099578/lai-zi-bei-da-suan-fa-ke-de-leetcodeti-j-wfdh/?envType=study-plan-v2&envId=top-100-liked

  • https://leetcode.cn/problems/group-anagrams/solutions/520469/zi-mu-yi-wei-ci-fen-zu-by-leetcode-solut-gyoc/?envType=study-plan-v2&envId=top-100-liked

解题方法

  1. 模式识别:根据相同特征进行元素分组,哈希表适用。以相同的特征为key,同组元素构成 value;
  2. python下一些字符串的操作方法,如 join(), sorted();
  3. python下基于dict的哈希表的操作,如检查内容存在与否,存储内容等;
  4. 存储与输出对数据结构的要求,list of list!

额外讨论

关于题目涉及的 python中的字符串操作

The line s_ = "".join(sorted(s)) in the Python code is a concise way to sort the characters of a string s and then concatenate them back into a single string. This line is crucial for the algorithm to group anagrams. Let’s break it down:

  1. sorted(s):
  • sorted() is a built-in Python function that takes an iterable (like a string, list, etc.) and returns a new list with the elements sorted in ascending order.

  • When sorted() is applied to a string, it treats the string as a sequence of characters and returns a list of characters sorted in alphabetical order.

  • For example, if s = 'eat', sorted(s) will return ['a', 'e', 't'].

  1. "".join(...):
  • join() is a string method in Python that takes an iterable as an argument and concatenates its elements into a single string. The string on which join() is called is used as the separator between elements.

  • In this case, "".join(...) uses an empty string ("") as the separator, meaning that it will simply concatenate the elements of the iterable without any additional characters in between.

  • This step is necessary because sorted(s) returns a list, and we need to convert this list back into a string.

So, s_ = "".join(sorted(s)) transforms the original string s into a new string s_ whose characters are the sorted characters of s. This is useful for grouping anagrams because anagrams will always result in the same string when their characters are sorted. For instance, ‘eat’, ‘tea’, and ‘ate’ will all be transformed into ‘aet’ after applying this operation.

复杂度

时间复杂度:

添加时间复杂度, 示例: O ( n ) O(n) O(n)

空间复杂度:

添加空间复杂度, 示例: O ( n ) O(n) O(n)

Code

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        # 根据特征进行归类,使用散列表(hash table)
        # 由于互为字母异位词的两个字符串包含的字母相同,因此对两个字符串分别进行排序之后得到的字符串一定是相同的,故可以将排序之后的字符串作为哈希表的键。 —— 力扣官方题解
        
        # 官方题解构建hash table的方法首先就没看明白,还是下面这种适合我
        hashtab = {} # 先创建一个 hash table

        for s in strs:

            s_ = "".join(sorted(s)) # 根据 s 的 sorted(s),形成 hash table 的 key

            if s_ not in hashtab: # if key in dict,通过 key 来 check 
                hashtab[s_] = [s] # 如果不在,就存入第一个,并且要存成 list: [s]
            else: 
                hashtab[s_].append(s) # 如果在,就直接在原有list后进行append
        
        return list(hashtab.values()) # 按照要求的list格式进行输出
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值