C# solution: dictionary


  • 0
    B
    public class Solution 
    {
        public IList<IList<string>> GroupAnagrams(string[] strs) 
        {
            var strKeyAndStrList = new Dictionary<string, IList<string>>();
    
            foreach(var str in strs)
            {
                var key = new string(str.ToArray().OrderBy(c => c).ToArray());
    
                if (!strKeyAndStrList.ContainsKey(key)) strKeyAndStrList[key] = new List<string>();
    
                strKeyAndStrList[key].Add(str);
            }
    
            var result = new List<IList<string>>();
    
            foreach(var value in strKeyAndStrList.Values)
            {
                result.Add(value);
            }
    
            return result;
        }
    }
    

Log in to reply
 

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