49. Group Anagrams
https://leetcode.com/problems/group-anagrams/submissions/
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]Thoughts
Code
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
m = collections.defaultdict(list)
for s in strs:
# m[''.join(sorted(s))].append(s)
cnt = [0] * 26
for c in s:
cnt[ord(c) - ord('a')] += 1
m[tuple(cnt)].append(s)
return m.values()Analysis
Last updated