My 98.59% solution in c#


  • 0
    I
        public IList<IList<string>> GroupAnagrams(string[] strs) {
            if (strs == null || !strs.Any())
            {
                return new List<IList<string>>();
            }
            
            var groups = new Dictionary<string, List<string>>();
            
            string sorted = "";
            char [] rawKey ;
            List<string> container = null;
            
            foreach(string str in strs)
            {
                rawKey = str.ToCharArray();
                
                Array.Sort(rawKey);
                
                var key = string.Join("", rawKey);
                
                if(groups.ContainsKey(key))
                {
                    container = groups[key];
                }
                else
                {
                    container = new List<string>();
                    groups.Add(key, container);
                }
                
                container.Add(str);
            }
            
            var resultSet = new List<IList<string>>();
            
            foreach(var kvp in groups)
            {
                resultSet.Add(kvp.Value);
            }
            
            return resultSet;
        }
    }

Log in to reply
 

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