C# Soln that beats 91% with LINQ


  • 1
    V

    Pretty simple to understand. Just check all chars of the words are present in the hashset map of each row of the keyboard and I used a LINQ lambda expression for it

    public class Solution {
        public string[] FindWords(string[] words) {
            HashSet<char> top = new HashSet<char> {'Q','q','W','w','E','e','R','r','t','T','t','Y','y','U','u','I','i','O','o','P','p'};
            HashSet<char> middle = new HashSet<char> {'A','S','D','F','G','H','J','K','L','a','s','d','f','g','h','j','k','l'};
            HashSet<char> bottom = new HashSet<char> {'Z','X','C','V','B','N','M','z','x','c','v','b','n','m'};
            
            List<string> list = new List<string>();
            foreach(string w in words)
            {
                if(w.All(c => middle.Contains(c)) || 
                   w.All(d => top.Contains(d))    ||
                   w.All(e => bottom.Contains(e)))
                    list.Add(w);
            }
            
            return list.ToArray();;
        }
    }
    

Log in to reply
 

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