본문 바로가기
python-algorithm

leetcode 49. Group Anagrams

by 무적김두칠 2023. 2. 7.

https://leetcode.com/problems/group-anagrams/description/

 

Group Anagrams - LeetCode

Can you solve this real interview question? Group Anagrams - Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
import collections
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
            
        anagram = collections.defaultdict(list)
 
        for word in strs:
            anagram[''.join((sorted(word)))].append(word)
            
        return list(anagram.values())
cs
반응형

댓글