-
Notifications
You must be signed in to change notification settings - Fork 115
/
049-GroupAnagrams.cs
41 lines (37 loc) · 1.16 KB
/
049-GroupAnagrams.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----------------------------------------------------------------------------
// Runtime: 288ms
// Memory Usage: 38.3 MB
// Link: https://leetcode.com/submissions/detail/320910763/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _049_GroupAnagrams
{
public IList<IList<string>> GroupAnagrams(string[] strs)
{
var mapping = new Dictionary<string, IList<string>>();
var key = string.Empty;
char[] ch;
foreach (var str in strs)
{
ch = str.ToCharArray();
Array.Sort(ch);
key = new string(ch);
if (!mapping.ContainsKey(key))
{
mapping.Add(key, new List<string>());
}
mapping[key].Add(str);
}
var result = new List<IList<string>>();
foreach (var pair in mapping)
{
result.Add(pair.Value.OrderBy(s => s).ToList());
}
return result;
}
}
}