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 |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 5. Longest Palindromic Substring (0) | 2023.02.08 |
---|---|
백준 25630 팰린드롬 소떡소떡 (0) | 2023.02.08 |
leetcode 819. Most Common Word (0) | 2023.02.07 |
leetcode 937. Reorder Data in Log Files (0) | 2023.02.07 |
leetcode 1967. Number of Strings That Appear as Substrings in Word (2) | 2023.02.03 |
댓글