One line C# solution using Linq

  • 0

    Simply take the IEnumerable of strings, create a new "key" by forming a new string from the sorted characters of each string and group the original entries by the new key. Then map each group to an IList, and convert the IEnumerable of Groups to an IList.

    public class Solution {
        public IList<IList<string>> GroupAnagrams(string[] strs) 
            return (IList<IList<string>>)strs
                .GroupBy(str => new string(str.OrderBy(c => c).ToArray()))
                .Select(group => (IList<string>)group.ToList()).ToList();

Log in to reply

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