leetcode (22) - Ectopic letter word packet

Given a string array, ectopic word letters together. Ectopic word letters refer to the same letters, but arranged in different strings.

Example:

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

All inputs are lowercase letters.
The answer does not consider the output sequence

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/group-anagrams
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

My solution

Serve as a stopgap

from collections import defaultdict
class Solution:
    def groupAnagrams(self, strs):
        sets = defaultdict(lambda:[])
        for each in strs:

            sets[''.join(sorted(each))].append(each)
        #print(sets)
        ans = [each for each in sets.values()]
        #print(ans)
        return ans

Guess you like

Origin www.cnblogs.com/Lzqayx/p/12172276.html