Scribbling

LeetCode: 49. Group Anagrams 본문

Computer Science/Coding Test

LeetCode: 49. Group Anagrams

focalpoint 2021. 9. 8. 17:01
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        from collections import defaultdict
        dic = defaultdict(list)
        for s in strs:
            counter = [0] * 26
            for char in s:
                counter[ord(char) - ord('a')] += 1
            dic[tuple(counter)].append(s)
        return [x for x in dic.values()]

 

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 31. Next Permutation  (0) 2021.09.09
35. Search Insert Position  (0) 2021.09.08
LeetCode: 41. First Missing Positive  (0) 2021.09.08
LeetCode: 47. Permutations II  (0) 2021.09.07
LeetCode: 46. Permutations  (0) 2021.09.07