-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
33 lines (30 loc) · 1.04 KB
/
Solution.cs
File metadata and controls
33 lines (30 loc) · 1.04 KB
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
using System.Text;
namespace LeetCode.Medium.Problem49{
//49. Group Anagrams
//https://leetcode.com/problems/group-anagrams/
/*
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
*/
public class Solution {
public IList<IList<string>> GroupAnagrams(string[] strs) {
Dictionary<string,List<string>> dic = new Dictionary<string,List<string>>();
for (int i = 0; i < strs.Length; i++)
{
var word = new string(strs[i].OrderBy(p => p).ToArray());
if (dic.ContainsKey(word))
dic[word].Add(strs[i]);
else
dic.Add(word,new List<string>() {strs[i]});
}
string[][] ans = new string[dic.Count][];
int index = 0;
foreach (var item in dic)
{
ans[index] = item.Value.ToArray();
index++;
}
return ans;
}
}
}