C# Solution


  • 0
    B
    public class Solution 
    {
        public string[] FindWords(string[] words) 
        {
            var result = new List<string>();
    
            if (words.Length == 0) return result.ToArray();
    
            var keyboard = new HashSet<char>[3];
            keyboard[0] = new HashSet<char>(){'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
            keyboard[1] = new HashSet<char>(){'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
            keyboard[2] = new HashSet<char>(){'z', 'x', 'c', 'v', 'b', 'n', 'm'};
    
    
    
            foreach(var word in words)
            {
                if (word == "") continue;
                var curRow = -1;
    
                foreach(var c in word)
                {
                    if (curRow == -1)
                    {
                        for(int i = 0; i < keyboard.Length; i++)
                        {
                            if (keyboard[i].Contains(char.ToLower(c)))
                            {
                                if (curRow == -1)    curRow = i;
                            }
                        }                   
                    }
                    else
                    {
                        if (!keyboard[curRow].Contains(char.ToLower(c)))
                        {
                            curRow = -1;
                            break;
                        }
                    }
                }
    
                if (curRow != -1)
                {
                    result.Add(word);
                }
            }
    
            return result.ToArray();
        }
    }
    

Log in to reply
 

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