Skip to content

Commit

Permalink
Merge pull request #3645 from Srihari2222/develop_articles
Browse files Browse the repository at this point in the history
Sri Hari: Neetcode-150/added-5-articles
  • Loading branch information
neetcode-gh authored Oct 4, 2024
2 parents 9800d75 + 47ce740 commit 6abdbb2
Show file tree
Hide file tree
Showing 5 changed files with 1,938 additions and 0 deletions.
209 changes: 209 additions & 0 deletions articles/anagram-groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
## 1. Sorting

::tabs-start

```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res = defaultdict(list)
for s in strs:
sortedS = ''.join(sorted(s))
res[sortedS].append(s)
return res.values()
```

```java
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> res = new HashMap<>();
for (String s : strs) {
char[] charArray = s.toCharArray();
Arrays.sort(charArray);
String sortedS = new String(charArray);
res.putIfAbsent(sortedS, new ArrayList<>());
res.get(sortedS).add(s);
}
return new ArrayList<>(res.values());
}
}
```

```cpp
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> res;
for (const auto& s : strs) {
string sortedS = s;
sort(sortedS.begin(), sortedS.end());
res[sortedS].push_back(s);
}
vector<vector<string>> result;
for (auto& pair : res) {
result.push_back(pair.second);
}
return result;
}
};
```
```javascript
class Solution {
/**
* @param {string[]} strs
* @return {string[][]}
*/
groupAnagrams(strs) {
const res = {};
for (let s of strs) {
const sortedS = s.split('').sort().join('');
if (!res[sortedS]) {
res[sortedS] = [];
}
res[sortedS].push(s);
}
return Object.values(res);
}
}
```

```csharp
public class Solution {
public List<List<string>> GroupAnagrams(string[] strs) {
var res = new Dictionary<string, List<string>>();
foreach (var s in strs) {
char[] charArray = s.ToCharArray();
Array.Sort(charArray);
string sortedS = new string(charArray);
if (!res.ContainsKey(sortedS)) {
res[sortedS] = new List<string>();
}
res[sortedS].Add(s);
}
return res.Values.ToList<List<string>>();
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(m * n \log n)$
* Space complexity: $O(m * n)$

> Where $m$ is the number of strings and $n$ is the length of the longest string.

---

## 2. Hash Table

::tabs-start

```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res = defaultdict(list)
for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
res[tuple(count)].append(s)
return res.values()
```
```java
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> res = new HashMap<>();
for (String s : strs) {
int[] count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
String key = Arrays.toString(count);
res.putIfAbsent(key, new ArrayList<>());
res.get(key).add(s);
}
return new ArrayList<>(res.values());
}
}
```

```cpp
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> res;
for (const auto& s : strs) {
vector<int> count(26, 0);
for (char c : s) {
count[c - 'a']++;
}
string key = to_string(count[0]);
for (int i = 1; i < 26; ++i) {
key += ',' + to_string(count[i]);
}
res[key].push_back(s);
}
vector<vector<string>> result;
for (const auto& pair : res) {
result.push_back(pair.second);
}
return result;
}
};
```
```javascript
class Solution {
/**
* @param {string[]} strs
* @return {string[][]}
*/
groupAnagrams(strs) {
const res = {};
for (let s of strs) {
const count = new Array(26).fill(0);
for (let c of s) {
count[c.charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
}
const key = count.join(',');
if (!res[key]) {
res[key] = [];
}
res[key].push(s);
}
return Object.values(res);
}
}
```

```csharp
public class Solution {
public List<List<string>> GroupAnagrams(string[] strs) {
var res = new Dictionary<string, List<string>>();
foreach (var s in strs) {
int[] count = new int[26];
foreach (char c in s) {
count[c - 'a']++;
}
string key = string.Join(",", count);
if (!res.ContainsKey(key)) {
res[key] = new List<string>();
}
res[key].Add(s);
}
return res.Values.ToList<List<string>>();
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(m * n)$
* Space complexity: $O(m)$

> Where $m$ is the number of strings and $n$ is the length of the longest string.
Loading

0 comments on commit 6abdbb2

Please sign in to comment.