-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
61 lines (60 loc) · 2.35 KB
/
Program.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//创建hashMap字典,key为string,value为List<string>。用于存储相同字符的单词。
//遍历strs中每一个单词word,把他们转化成CharArray后进行排序,再转化成字符串。
//如果这时字典中不存在key为该字符串,创建该字符串的key,将对应的value设为new List<string>,再将此时遍历到的word的添加到List中、
//如果这时字典中已存在key为该字符串,则直接将遍历到的word添加到List中。
//比那里结束后设置List<string>的Array接收结果。其长度设为字典中键值对的个数。
using System;
using System.Collections.Generic;
namespace GroupAnagrams
{
class Program
{
static void Main(string[] args)
{
string[] strs = { "eat", "tea", "tan", "ate", "nat", "bat" };
GroupAnagrams(strs);
}
static IList<IList<string>> GroupAnagrams_Sort(string[] strs)
{
var hashMap = new Dictionary<string, List<string>>();
foreach (var word in strs)
{
var tem = word.ToCharArray();
Array.Sort(tem);
String str = new string(tem);
if (!hashMap.ContainsKey(str))
{
hashMap.Add(str, new List<string>());
hashMap[str].Add(word);
}
else
hashMap[str].Add(word);
}
var res = new List<string>[hashMap.Count];
Console.WriteLine(res[0]);
int i = 0;
foreach (var words in hashMap)
{
res[i] = words.Value;
i++;
}
return res;
}
static IList<IList<string>> GroupAnagrams(string[] strs)
{
var dict = new Dictionary<string, List<string>>();
foreach (var str in strs)
{
var letters = new int[26];
foreach (var letter in str)
letters[letter - 'a']++;
var key = letters.Aggregate("", (current, letter) => current + '-' + letter);
if (!dict.ContainsKey(key))
dict[key] = new List<string>();
dict[key].Add(str);
}
var res = dict.Select(kv => kv.Value).Cast<IList<string>>().ToList();
return res;
}
}
}