|
1 |
| -# Group Anagrams (Medium) |
| 1 | +# Anagram Groups (Medium) |
2 | 2 |
|
3 | 3 | ## Table of Contents
|
4 | 4 |
|
5 | 5 | - [Problem Statement](#problem-statement)
|
6 | 6 | - [Examples](#examples)
|
7 | 7 | - [Constraints](#constraints)
|
8 | 8 | - [Solutions](#solutions)
|
9 |
| - - [Approach 1: Using Sorted Strings as Keys](#approach-1-using-sorted-strings-as-keys) |
10 |
| - - [Approach 2: Hash Map with Character Count as Key](#approach-2-hash-map-with-character-count-as-key) |
| 9 | + - [Approach 1: Sorting Characters](#approach-1-sorting-characters) |
| 10 | + - [Approach 2: Character Count](#approach-2-character-count) |
11 | 11 | - [Complexity Analysis](#complexity-analysis)
|
12 | 12 | - [Code Explanation](#code-explanation)
|
13 | 13 | - [Related Resources](#related-resources)
|
@@ -49,58 +49,57 @@ Output: [["a"]]
|
49 | 49 |
|
50 | 50 | ## Solutions
|
51 | 51 |
|
52 |
| -### Approach 1: Using Sorted Strings as Keys |
| 52 | +### Approach 1: Sorting Characters |
53 | 53 |
|
54 | 54 | ```python
|
55 | 55 | class Solution:
|
56 |
| - def twoSum(self, nums: List[int], target: int) -> List[int]: |
57 |
| - if len(nums) == 2: |
58 |
| - return [0, 1] |
59 |
| - for i in range(len(nums)): |
60 |
| - for j in range(i + 1, len(nums)): |
61 |
| - if nums[i] + nums[j] == target: |
62 |
| - if nums.index(nums[i]) == nums.index(nums[j]): |
63 |
| - return [nums.index(nums[i]), nums.index(nums[j], i + 1, len(nums))] |
64 |
| - return [nums.index(nums[i]), nums.index(nums[j])] |
| 56 | + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: |
| 57 | + anagrams = defaultdict(list) |
| 58 | + for word in strs: |
| 59 | + sorted_word = ''.join(sorted(word)) |
| 60 | + anagrams[sorted_word].append(word) |
| 61 | + return list(anagrams.values()) |
65 | 62 | ```
|
66 | 63 |
|
67 |
| -### Approach 2: Hash Map with Character Count as Key |
| 64 | +### Approach 2: Character Count |
68 | 65 |
|
69 | 66 | ```python
|
70 | 67 | class Solution:
|
71 |
| - def twoSum(self, nums: List[int], target: int) -> List[int]: |
72 |
| - seenMap = {} |
73 |
| - for i in range(len(nums)): |
74 |
| - remain = target - nums[i] |
75 |
| - if remain in seenMap: |
76 |
| - return [seenMap[remain], i] |
77 |
| - seenMap[nums[i]] = i |
78 |
| - return nums |
| 68 | + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: |
| 69 | + ans = collections.defaultdict(list) |
| 70 | + for s in strs: |
| 71 | + count = [0] * 26 |
| 72 | + for c in s: |
| 73 | + count[ord(c) - ord("a")] += 1 |
| 74 | + ans[tuple(count)].append(s) |
| 75 | + return ans.values() |
79 | 76 | ```
|
80 | 77 |
|
81 | 78 | ## Complexity Analysis
|
82 | 79 |
|
83 |
| -### Approach 1: |
84 |
| -- Time Complexity: $O(N * M * \log(M))$, where N is the number of strings and M is the maximum length of a string. |
| 80 | +### Approach 1: Sorting Characters |
| 81 | +- Time Complexity: $O(N * M * log(M))$, where N is the number of strings and M is the maximum length of a string. |
85 | 82 | - Space Complexity: $O(N * M)$
|
86 | 83 |
|
87 |
| -### Approach 2: |
| 84 | +### Approach 2: Character Count |
88 | 85 | - Time Complexity: $O(N * M)$, where N is the number of strings and M is the maximum length of a string.
|
89 | 86 | - Space Complexity: $O(N * M)$
|
90 | 87 |
|
91 | 88 | ## Code Explanation
|
92 | 89 |
|
93 |
| -### Approach 1: Using Sorted Strings as Keys |
94 |
| -This approach involves sorting each string and using the sorted string as a key in a dictionary. |
95 |
| - |
96 |
| -### Approach 2: Hash Map with Character Count as Key |
97 |
| -This solution uses a hash map to group anagrams: |
98 |
| -1. We iterate through each string in the input array. |
99 |
| -2. For each string, we create a key by counting the occurrences of each character. |
100 |
| -3. We use this count as a key in our hash map. The value is a list of all strings that have the same character count. |
101 |
| -4. Finally, we return the values of the hash map, which are the grouped anagrams. |
102 |
| - |
103 |
| -This approach is efficient and passes on both Neetcode.io and LeetCode. |
| 90 | +### Approach 1: Sorting Characters |
| 91 | +1. We use a defaultdict to group anagrams. |
| 92 | +2. For each word, we sort its characters to create a key. |
| 93 | +3. We append the original word to the list associated with its sorted key. |
| 94 | +4. Finally, we return the values of the dictionary as a list of lists. |
| 95 | + |
| 96 | +### Approach 2: Character Count |
| 97 | +1. We use a defaultdict to group anagrams. |
| 98 | +2. For each word, we create a count array of length 26 (for lowercase English letters). |
| 99 | +3. We count the occurrences of each character in the word. |
| 100 | +4. We use the tuple of this count array as a key to group anagrams. |
| 101 | +5. We append the original word to the list associated with its count key. |
| 102 | +6. Finally, we return the values of the dictionary. |
104 | 103 |
|
105 | 104 | ## Related Resources
|
106 | 105 |
|
|
0 commit comments