Leetcode Hot 100 & 49. Group Anagrams

  Written in front:

  Unknowingly, I have already researched twice. Since I choose to take the AI ​​road in the future, I will inevitably have to brush up the leetcode questions, because the written test is always used when recruiting. First of all, brush up the hot 100 questions of leetcode. A good memory cannot keep up with a bad pen. Record the gains in writing these algorithm questions for yourself.

  References:

  Test site: Hash & [ question stem ]

  This question was not solved, the first submission was wrong, and the second submission timed out.

  1. Two wrong ideas

  The main idea of ​​this question is actually very clear. "abc" and "bca" are synonyms, and they should be divided into one category and placed in the same list. So how to construct this kind of data structure that can contain the invariant property of sorting? The first thing I thought of was to use a collection, that is, set, and wrote the following code:

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        result_dict = {}

        for onestr in strs:
            onestr_set = set([s for s in onestr])
            
            FLAG = True
            
            for ref_key in result_dict.keys():
                ref_key_set = set([s for s in ref_key])
                if onestr_set == ref_key_set:
                    result_dict[ref_key].append(onestr)
                    FLAG = False
            
            if FLAG:
                result_dict[onestr] = [onestr]
                
            # print(onestr)

        result = []
        for value in result_dict.values():
            result.append(value)

        return result

  But an error was reported, because there is a pit in the question stem, that is, each letter does not appear only once, so the sets corresponding to ["daaa"] and ["aaad"] are ("a", "d") , but not an anagram.

  Since a collection cannot be used as an identifier for anagrams, use a dictionary. The key of the dictionary is the anagram that appears for the first time. If there is no anagram later, it will be classified as append, but this time it is timed out. . Only passed 111/118, as follows:

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        result_dict = {}

        for onestr in strs:
            onestr_set = dict()
            for s in onestr:
                if s in onestr_set.keys():
                    onestr_set[s] += 1
                else:
                    onestr_set[s] = 1
            
            FLAG = True
            
            for ref_key in result_dict.keys():
                ref_key_set = dict()
                for s in ref_key:
                    if s in ref_key_set.keys():
                        ref_key_set[s] += 1
                    else:
                        ref_key_set[s] = 1
                if onestr_set == ref_key_set:
                    result_dict[ref_key].append(onestr)
                    FLAG = False
            
            if FLAG:
                result_dict[onestr] = [onestr]
                
            # print(onestr)

        result = []
        for value in result_dict.values():
            result.append(value)

        return result

  2. The right way of thinking

  In fact, I overlooked a crucial property, that is, as long as anagrams are sorted by strings, they will have unique keys. Using this property, the following correct code can be written:

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        onestr_set = collections.defaultdict(list)

        for onestr in strs:
            onestr_set["".join(sorted(onestr))].append(onestr)
            # print(onestr)

        result = []
        for value in onestr_set.values():
            result.append(value)
            
        return(result)

  It can be seen that the correct solution also uses a dictionary to classify anagrams (this is obvious, because the output is a list one by one), and the others are nothing special

  3. Small tips

  读官方题解的时候对collections.defaultdict这个点有点好奇,collections这个东西我是知道的,里面有一些扩展的数据结构,具体是哪些数据结构我就不知道了。普通的字典在查询的键不存在时会抛出错误,而collections.defaultdict在查询到不存在的键时会创建,默认使用的是这个方法传递的一个method即list,运行时它会调用list()生成一个空的列表,就是这样。

おすすめ

転載: blog.csdn.net/weixin_43590796/article/details/131079692