forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_anagram.py
28 lines (22 loc) · 810 Bytes
/
group_anagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
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, typically using all the original letters exactly once.
Example:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
'''
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
from collections import defaultdict
anagrams = defaultdict(list)
for s in strs:
anagrams[str(sorted(s))].append(s)
return list(anagrams.values())
n=["eat", "tea", "tan", "ate", "nat", "bat"]
ans=Solution()
res=ans.groupAnagrams(n)
print(res)