-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 18 ms (39.42%), Memory - 22.1 MB (…
…12.02%)
- Loading branch information
Showing
2 changed files
with
43 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
# Approach 1 - Categorize by Sorted String | ||
# Approach 2 - Categorize by count | ||
|
||
# Time: O(n k log k), n = length of strs, k = max lenth of string in strs | ||
# Space: O(n*k) | ||
# Time: O(n * k), n = length of strs, k = max lenth of string in strs | ||
# Space: O(n * k) | ||
|
||
from collections import defaultdict | ||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
hashmap = defaultdict(list) | ||
ans = defaultdict(list) | ||
|
||
for s in strs: | ||
hashmap[tuple(sorted(s))].append(s) | ||
|
||
return hashmap.values() | ||
count = [0] * 26 | ||
for c in s: | ||
count[(ord(c) - ord('a'))] += 1 | ||
ans[tuple(count)].append(s) | ||
|
||
return list(ans.values()) | ||
|