C# Solution


  • 0
    public class Solution {
        public IList<IList<string>> GroupStrings(string[] strings) {
            var result = new List<IList<string>>();
            if (strings == null || strings.Length == 0) return result;
            
            var group = new Dictionary<string, List<string>>();
            foreach(var str in strings) {
                var baseStr = GetBaseString(str);
                if (!group.ContainsKey(baseStr)) group[baseStr] = new List<string>();
                group[baseStr].Add(str);
            }
            
            foreach(var subResult in group.Values) result.Add(subResult);        
            return result;
        }
        
        public string GetBaseString(string str) {
            var chars = str.ToCharArray();
            var diff = chars[0] - 'a';
            for(int i = 0; i < chars.Length; i++) {
                chars[i] = (char)(chars[i] - diff);
                chars[i] = chars[i] < 'a' ? (char)(chars[i] + 26) : chars[i];
            }
            return new string(chars);
        }
    }
    

Log in to reply
 

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