Clear and easy solution. C# LINQ. Beats 94%.


  • 1
    K
     var result = new List<string>();
    
                var rows = new char[3][];
    
                rows[0] = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' };
                rows[1] = new char[] { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' };
                rows[2] = new char[] { 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
    
                for (int i = 0; i < words.Length; i++)
                {
                    var letters = words[i]
                        .Distinct()
                        .Select(letter => Char.ToLower(letter));
    
                    foreach (var row in rows)
                    {
                        if (letters.All(letter => row.Contains(letter)))
                        {
                            result.Add(words[i]);
                        }
    
                    }
                }
    
                return result.ToArray();
    

Log in to reply
 

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