LeetCode 솔루션 분류
[10/28] 49. Group Anagrams
본문
Medium
12974387Add to ListShareGiven 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, typically using all the original letters exactly once.
Example 1:
Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input: strs = [""] Output: [[""]]
Example 3:
Input: strs = ["a"] Output: [["a"]]
Constraints:
1 <= strs.length <= 104
0 <= strs[i].length <= 100
strs[i]
consists of lowercase English letters.
Accepted
1,704,675
Submissions
2,568,825
관련자료
-
링크
댓글 1
학부유학생님의 댓글
- 익명
- 작성일
Runtime: 244 ms, faster than 35.26% of Python3 online submissions for Group Anagrams.
Memory Usage: 18.1 MB, less than 53.63% of Python3 online submissions for Group Anagrams.
Memory Usage: 18.1 MB, less than 53.63% of Python3 online submissions for Group Anagrams.
from collections import Counter, defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ga = defaultdict(list)
for word in strs:
ga[tuple(sorted(list(word)))].append(word)
return ga.values()