Created
July 12, 2017 00:21
-
-
Save jianminchen/cadccaccb75589c92d0abe427f7e67e7 to your computer and use it in GitHub Desktop.
Leetcode 49 - group anagrams - study code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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