November 9, 2024
Working on hashmap problems now.
1from collections import defaultdict2class Solution:3 def groupAnagrams(self, strs: List[str]) -> List[List[str]]:4 indexToSortedWord = defaultdict(list)5 for i, word in enumerate(strs):6 indexToSortedWord[str(sorted(word))].append(i)78 result = []9 for groupIdx in indexToSortedWord.values():10 group = []11 for idx in groupIdx:12 group.append(strs[idx])13 result.append(group)14 return result
O(n) = n * m * log(m) time complexity, and O(n) = n space complexity.
LeetCode 289. Game of Life
LeetCode 56. Merge Intervals