Intuition
Create a map os strings, group by it’s sorted version.
Approach
Create a map os strings, group by it’s sorted version.
Complexity
-
Time complexity: O(n logn)
-
Space complexity: O(n)
Code
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
_map = {}
for s in strs:
_sorted = "".join(sorted(s))
if _sorted in _map:
_map[_sorted].append(s)
else:
_map[_sorted] = [s]
return _map.values()