Skip to content

Commit 56174af

Browse files
committed
feat: add solutions for lc No.0049
1 parent 8415a7e commit 56174af

7 files changed

Lines changed: 198 additions & 190 deletions

File tree

solution/0000-0099/0049.Group Anagrams/README.md

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -180,77 +180,34 @@ use std::collections::HashMap;
180180

181181
impl Solution {
182182
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
183-
let mut map = HashMap::new();
183+
let mut d = HashMap::new();
184184
for s in strs {
185-
let key = {
186-
let mut arr = s.chars().collect::<Vec<char>>();
187-
arr.sort();
188-
arr.iter().collect::<String>()
189-
};
190-
let val = map.entry(key).or_insert(vec![]);
191-
val.push(s);
185+
let mut t: Vec<char> = s.chars().collect();
186+
t.sort_unstable();
187+
let k: String = t.into_iter().collect();
188+
d.entry(k).or_insert_with(Vec::new).push(s);
192189
}
193-
map.into_iter().map(|(_, v)| v).collect()
190+
d.into_values().collect()
194191
}
195192
}
196193
```
197194

198195
#### C#
199196

200197
```cs
201-
using System.Collections.Generic;
202-
203-
public class Comparer : IEqualityComparer<string>
204-
{
205-
public bool Equals(string left, string right)
206-
{
207-
if (left.Length != right.Length) return false;
208-
209-
var leftCount = new int[26];
210-
foreach (var ch in left)
211-
{
212-
++leftCount[ch - 'a'];
213-
}
214-
215-
var rightCount = new int[26];
216-
foreach (var ch in right)
217-
{
218-
var index = ch - 'a';
219-
if (++rightCount[index] > leftCount[index]) return false;
220-
}
221-
222-
return true;
223-
}
224-
225-
public int GetHashCode(string obj)
226-
{
227-
var hashCode = 0;
228-
for (int i = 0; i < obj.Length; ++i)
229-
{
230-
hashCode ^= 1 << (obj[i] - 'a');
231-
}
232-
return hashCode;
233-
}
234-
}
235-
236198
public class Solution {
237199
public IList<IList<string>> GroupAnagrams(string[] strs) {
238-
var dict = new Dictionary<string, List<string>>(new Comparer());
239-
foreach (var str in strs)
240-
{
241-
List<string> list;
242-
if (!dict.TryGetValue(str, out list))
243-
{
244-
list = new List<string>();
245-
dict.Add(str, list);
200+
var d = new Dictionary<string, List<string>>();
201+
foreach (string s in strs) {
202+
char[] t = s.ToCharArray();
203+
Array.Sort(t);
204+
string k = new string(t);
205+
if (!d.ContainsKey(k)) {
206+
d[k] = new List<string>();
246207
}
247-
list.Add(str);
248-
}
249-
foreach (var list in dict.Values)
250-
{
251-
list.Sort();
208+
d[k].Add(s);
252209
}
253-
return new List<IList<string>>(dict.Values);
210+
return new List<IList<string>>(d.Values);
254211
}
255212
}
256213
```
@@ -357,12 +314,61 @@ func groupAnagrams(strs []string) (ans [][]string) {
357314

358315
```ts
359316
function groupAnagrams(strs: string[]): string[][] {
360-
const map = new Map<string, string[]>();
361-
for (const str of strs) {
362-
const k = str.split('').sort().join('');
363-
map.set(k, (map.get(k) ?? []).concat([str]));
317+
const d = new Map<string, string[]>();
318+
for (const s of strs) {
319+
const cnt = new Array(26).fill(0);
320+
for (const c of s) {
321+
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
322+
}
323+
const key = cnt.join(',');
324+
if (!d.has(key)) {
325+
d.set(key, []);
326+
}
327+
d.get(key)!.push(s);
328+
}
329+
return Array.from(d.values());
330+
}
331+
```
332+
333+
#### Rust
334+
335+
```rust
336+
use std::collections::HashMap;
337+
338+
impl Solution {
339+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
340+
let mut d = HashMap::new();
341+
for s in strs {
342+
let mut cnt = [0; 26];
343+
for c in s.chars() {
344+
cnt[(c as usize) - ('a' as usize)] += 1;
345+
}
346+
d.entry(cnt).or_insert_with(Vec::new).push(s);
347+
}
348+
d.into_values().collect()
349+
}
350+
}
351+
```
352+
353+
#### C#
354+
355+
```cs
356+
public class Solution {
357+
public IList<IList<string>> GroupAnagrams(string[] strs) {
358+
var d = new Dictionary<string, List<string>>();
359+
foreach (string s in strs) {
360+
int[] cnt = new int[26];
361+
foreach (char c in s) {
362+
cnt[c - 'a']++;
363+
}
364+
string key = string.Join(",", cnt);
365+
if (!d.ContainsKey(key)) {
366+
d[key] = new List<string>();
367+
}
368+
d[key].Add(s);
369+
}
370+
return new List<IList<string>>(d.Values);
364371
}
365-
return [...map.values()];
366372
}
367373
```
368374

solution/0000-0099/0049.Group Anagrams/README_EN.md

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -178,77 +178,34 @@ use std::collections::HashMap;
178178

179179
impl Solution {
180180
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
181-
let mut map = HashMap::new();
181+
let mut d = HashMap::new();
182182
for s in strs {
183-
let key = {
184-
let mut arr = s.chars().collect::<Vec<char>>();
185-
arr.sort();
186-
arr.iter().collect::<String>()
187-
};
188-
let val = map.entry(key).or_insert(vec![]);
189-
val.push(s);
183+
let mut t: Vec<char> = s.chars().collect();
184+
t.sort_unstable();
185+
let k: String = t.into_iter().collect();
186+
d.entry(k).or_insert_with(Vec::new).push(s);
190187
}
191-
map.into_iter().map(|(_, v)| v).collect()
188+
d.into_values().collect()
192189
}
193190
}
194191
```
195192

196193
#### C#
197194

198195
```cs
199-
using System.Collections.Generic;
200-
201-
public class Comparer : IEqualityComparer<string>
202-
{
203-
public bool Equals(string left, string right)
204-
{
205-
if (left.Length != right.Length) return false;
206-
207-
var leftCount = new int[26];
208-
foreach (var ch in left)
209-
{
210-
++leftCount[ch - 'a'];
211-
}
212-
213-
var rightCount = new int[26];
214-
foreach (var ch in right)
215-
{
216-
var index = ch - 'a';
217-
if (++rightCount[index] > leftCount[index]) return false;
218-
}
219-
220-
return true;
221-
}
222-
223-
public int GetHashCode(string obj)
224-
{
225-
var hashCode = 0;
226-
for (int i = 0; i < obj.Length; ++i)
227-
{
228-
hashCode ^= 1 << (obj[i] - 'a');
229-
}
230-
return hashCode;
231-
}
232-
}
233-
234196
public class Solution {
235197
public IList<IList<string>> GroupAnagrams(string[] strs) {
236-
var dict = new Dictionary<string, List<string>>(new Comparer());
237-
foreach (var str in strs)
238-
{
239-
List<string> list;
240-
if (!dict.TryGetValue(str, out list))
241-
{
242-
list = new List<string>();
243-
dict.Add(str, list);
198+
var d = new Dictionary<string, List<string>>();
199+
foreach (string s in strs) {
200+
char[] t = s.ToCharArray();
201+
Array.Sort(t);
202+
string k = new string(t);
203+
if (!d.ContainsKey(k)) {
204+
d[k] = new List<string>();
244205
}
245-
list.Add(str);
246-
}
247-
foreach (var list in dict.Values)
248-
{
249-
list.Sort();
206+
d[k].Add(s);
250207
}
251-
return new List<IList<string>>(dict.Values);
208+
return new List<IList<string>>(d.Values);
252209
}
253210
}
254211
```
@@ -355,12 +312,61 @@ func groupAnagrams(strs []string) (ans [][]string) {
355312

356313
```ts
357314
function groupAnagrams(strs: string[]): string[][] {
358-
const map = new Map<string, string[]>();
359-
for (const str of strs) {
360-
const k = str.split('').sort().join('');
361-
map.set(k, (map.get(k) ?? []).concat([str]));
315+
const d = new Map<string, string[]>();
316+
for (const s of strs) {
317+
const cnt = new Array(26).fill(0);
318+
for (const c of s) {
319+
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
320+
}
321+
const key = cnt.join(',');
322+
if (!d.has(key)) {
323+
d.set(key, []);
324+
}
325+
d.get(key)!.push(s);
326+
}
327+
return Array.from(d.values());
328+
}
329+
```
330+
331+
#### Rust
332+
333+
```rust
334+
use std::collections::HashMap;
335+
336+
impl Solution {
337+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
338+
let mut d = HashMap::new();
339+
for s in strs {
340+
let mut cnt = [0; 26];
341+
for c in s.chars() {
342+
cnt[(c as usize) - ('a' as usize)] += 1;
343+
}
344+
d.entry(cnt).or_insert_with(Vec::new).push(s);
345+
}
346+
d.into_values().collect()
347+
}
348+
}
349+
```
350+
351+
#### C#
352+
353+
```cs
354+
public class Solution {
355+
public IList<IList<string>> GroupAnagrams(string[] strs) {
356+
var d = new Dictionary<string, List<string>>();
357+
foreach (string s in strs) {
358+
int[] cnt = new int[26];
359+
foreach (char c in s) {
360+
cnt[c - 'a']++;
361+
}
362+
string key = string.Join(",", cnt);
363+
if (!d.ContainsKey(key)) {
364+
d[key] = new List<string>();
365+
}
366+
d[key].Add(s);
367+
}
368+
return new List<IList<string>>(d.Values);
362369
}
363-
return [...map.values()];
364370
}
365371
```
366372

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
1-
using System.Collections.Generic;
2-
3-
public class Comparer : IEqualityComparer<string>
4-
{
5-
public bool Equals(string left, string right)
6-
{
7-
if (left.Length != right.Length) return false;
8-
9-
var leftCount = new int[26];
10-
foreach (var ch in left)
11-
{
12-
++leftCount[ch - 'a'];
13-
}
14-
15-
var rightCount = new int[26];
16-
foreach (var ch in right)
17-
{
18-
var index = ch - 'a';
19-
if (++rightCount[index] > leftCount[index]) return false;
20-
}
21-
22-
return true;
23-
}
24-
25-
public int GetHashCode(string obj)
26-
{
27-
var hashCode = 0;
28-
for (int i = 0; i < obj.Length; ++i)
29-
{
30-
hashCode ^= 1 << (obj[i] - 'a');
31-
}
32-
return hashCode;
33-
}
34-
}
35-
361
public class Solution {
372
public IList<IList<string>> GroupAnagrams(string[] strs) {
38-
var dict = new Dictionary<string, List<string>>(new Comparer());
39-
foreach (var str in strs)
40-
{
41-
List<string> list;
42-
if (!dict.TryGetValue(str, out list))
43-
{
44-
list = new List<string>();
45-
dict.Add(str, list);
3+
var d = new Dictionary<string, List<string>>();
4+
foreach (string s in strs) {
5+
char[] t = s.ToCharArray();
6+
Array.Sort(t);
7+
string k = new string(t);
8+
if (!d.ContainsKey(k)) {
9+
d[k] = new List<string>();
4610
}
47-
list.Add(str);
11+
d[k].Add(s);
4812
}
49-
foreach (var list in dict.Values)
50-
{
51-
list.Sort();
52-
}
53-
return new List<IList<string>>(dict.Values);
13+
return new List<IList<string>>(d.Values);
5414
}
55-
}
15+
}

0 commit comments

Comments
 (0)