Skip to content

Commit 2a35fe0

Browse files
committed
Update README
1 parent 460489e commit 2a35fe0

File tree

1 file changed

+35
-36
lines changed
  • Neetcode/Neetcode_150/Group_Anagrams

1 file changed

+35
-36
lines changed

Neetcode/Neetcode_150/Group_Anagrams/README.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Group Anagrams (Medium)
1+
# Anagram Groups (Medium)
22

33
## Table of Contents
44

55
- [Problem Statement](#problem-statement)
66
- [Examples](#examples)
77
- [Constraints](#constraints)
88
- [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)
1111
- [Complexity Analysis](#complexity-analysis)
1212
- [Code Explanation](#code-explanation)
1313
- [Related Resources](#related-resources)
@@ -49,58 +49,57 @@ Output: [["a"]]
4949

5050
## Solutions
5151

52-
### Approach 1: Using Sorted Strings as Keys
52+
### Approach 1: Sorting Characters
5353

5454
```python
5555
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())
6562
```
6663

67-
### Approach 2: Hash Map with Character Count as Key
64+
### Approach 2: Character Count
6865

6966
```python
7067
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()
7976
```
8077

8178
## Complexity Analysis
8279

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.
8582
- Space Complexity: $O(N * M)$
8683

87-
### Approach 2:
84+
### Approach 2: Character Count
8885
- Time Complexity: $O(N * M)$, where N is the number of strings and M is the maximum length of a string.
8986
- Space Complexity: $O(N * M)$
9087

9188
## Code Explanation
9289

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.
104103

105104
## Related Resources
106105

0 commit comments

Comments
 (0)