simple c# solution with Dictionary


  • 0
    N
    public class Solution {
        public IList<IList<string>> GroupAnagrams(string[] strs) {
            var dict = new Dictionary<string, IList<string>>();
            foreach(var s in strs){
                var chars = s.ToCharArray();
                Array.Sort(chars);
                var key = new string(chars);
                if(!dict.ContainsKey(key)){
                    dict[key] = new  List<string>();
                }
                dict[key].Add(s);
            }
            var list = new List<IList<string>>();
            foreach(var kv in dict){
                list.Add(kv.Value);
            }
            
            return list;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.