public class Solution
    {
        public IList<IList<string>> GroupAnagrams(string[] strs)
        {
            var L = new List<IList<string>>();
            var T = new List<KeyValuePair<string, int>>();
            for (var i = 0; i < strs.Length; i++)
            {
                var list = new List<char>();
                var str = strs[i];
                foreach (var c in str)
                {
                    list.Add(c);
                }
                list = list.OrderBy(x => x).ToList();

                var temp = "";
                foreach (var c in list)
                {
                    temp += c;
                }
                T.Add(new KeyValuePair<string, int>(temp, i));
            }

            var G = T.GroupBy(x => x.Key).ToList();
            foreach (var g in G)
            {
                var K = new List<string>();
                foreach (var i in g)
                {
                    K.Add(strs[i.Value]);
                }
                L.Add(K);
            }
            return L;
        }
    }

 

posted on 2018-10-03 19:08  Sempron2800+  阅读(146)  评论(0编辑  收藏  举报