Skip to content

Commit 963e571

Browse files
committedMar 26, 2022
feat: add solutions to lcci problem: No.10.02
No.10.02.Group Anagrams
1 parent 1e55f6d commit 963e571

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
 

‎lcci/10.02.Group Anagrams/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,40 @@ func groupAnagrams(strs []string) [][]string {
118118
}
119119
```
120120

121+
### **TypeScript**
122+
123+
```ts
124+
function groupAnagrams(strs: string[]): string[][] {
125+
const map = new Map<string, string[]>();
126+
for (const s of strs) {
127+
const k = s.split('').sort().join();
128+
map.set(k, (map.get(k) || []).concat([s]));
129+
}
130+
return [...map.values()];
131+
}
132+
```
133+
134+
### **Rust**
135+
136+
```rust
137+
use std::collections::HashMap;
138+
139+
impl Solution {
140+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
141+
let mut map = HashMap::new();
142+
for s in strs {
143+
let key = {
144+
let mut cs = s.chars().collect::<Vec<char>>();
145+
cs.sort();
146+
cs.iter().collect::<String>()
147+
};
148+
map.entry(key).or_insert(vec![]).push(s);
149+
}
150+
map.into_values().collect()
151+
}
152+
}
153+
```
154+
121155
### **...**
122156

123157
```

‎lcci/10.02.Group Anagrams/README_EN.md

+35
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,41 @@ func groupAnagrams(strs []string) [][]string {
115115
}
116116
```
117117

118+
### **TypeScript**
119+
120+
```ts
121+
function groupAnagrams(strs: string[]): string[][] {
122+
const map = new Map<string, string[]>();
123+
for (const s of strs) {
124+
const k = s.split('').sort().join();
125+
map.set(k, (map.get(k) || []).concat([s]));
126+
}
127+
return [...map.values()];
128+
}
129+
```
130+
131+
### **Rust**
132+
133+
```rust
134+
use std::collections::HashMap;
135+
136+
impl Solution {
137+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
138+
let mut map = HashMap::new();
139+
for s in strs {
140+
let key = {
141+
let mut cs = s.chars().collect::<Vec<char>>();
142+
cs.sort();
143+
cs.iter().collect::<String>()
144+
};
145+
map.entry(key).or_insert(vec![]).push(s);
146+
}
147+
map.into_values().collect()
148+
}
149+
}
150+
```
151+
152+
118153
### **...**
119154

120155
```

‎lcci/10.02.Group Anagrams/Solution.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
5+
let mut map = HashMap::new();
6+
for s in strs {
7+
let key = {
8+
let mut cs = s.chars().collect::<Vec<char>>();
9+
cs.sort();
10+
cs.iter().collect::<String>()
11+
};
12+
map.entry(key).or_insert(vec![]).push(s);
13+
}
14+
map.into_values().collect()
15+
}
16+
}

‎lcci/10.02.Group Anagrams/Solution.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function groupAnagrams(strs: string[]): string[][] {
2+
const map = new Map<string, string[]>();
3+
for (const s of strs) {
4+
const k = s.split('').sort().join();
5+
map.set(k, (map.get(k) || []).concat([s]));
6+
}
7+
return [...map.values()];
8+
}

0 commit comments

Comments
 (0)