Sort string to calculate the key of this string.
Time Complexity : O( N * K * Log(K) ). N is number of strings. K is maximum length of string.
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
seen = defaultdict(list)
for s in strs:
seen[tuple(sorted(s))].append(s)
return seen.values()
No comments:
Post a Comment