Short C# solution using Dictionary and Lambda Expression :)


  • 1
    D
    public class Solution {
        public List<string> Anagrams(string[] strs) {
            var book = new Dictionary<string, List<string>>();
            foreach(var str in strs){
                var s = str.ToCharArray();
                Array.Sort(s);
                var key = new string(s);
                if (!book.ContainsKey(key)){
                    book.Add(key, new List<string>());
                }
                book[key].Add(str);
            }
            return book.Values.Where(v => v.Count > 1).SelectMany(i=>i).ToList();
        }
    }

Log in to reply
 

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