Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/cadccaccb75589c92d0abe427f7e67e7 to your computer and use it in GitHub Desktop.
Save jianminchen/cadccaccb75589c92d0abe427f7e67e7 to your computer and use it in GitHub Desktop.
Leetcode 49 - group anagrams - study code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leetcode49_groupAnagrams
{
/// <summary>
/// Leetcode 49
/// study code submission in the discussion
/// https://discuss.leetcode.com/topic/24494/share-my-short-java-solution
/// </summary>
class Program
{
static void Main(string[] args)
{
}
public List<List<String>> groupAnagrams(String[] strs) {
if(strs == null || strs.Length == 0){
return new List<List<String>>();
}
var map = new Dictionary<String, List<String>>();
foreach (String s in strs) {
char[] ca = s.ToCharArray();
Array.Sort(ca);
String keyStr = new string(ca);
if(!map.ContainsKey(keyStr))
{
map.Add(keyStr, new List<String>());
}
map[keyStr].Add(s);
}
return new List<List<String>>(map.Values);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment