Simple C# solution


  • 0
    K

    string [] keyboard = {"QWERTYUIOP" , "ASDFGHJKL" , "ZXCVBNM"};

    public string[] FindWords(string[] words) {
          
        List<string> output = new List<string>();
        bool allFound = false;
        foreach(string w in words){
            string word = w.ToUpper();
            char c = word[0];
            int k = GetKeyboardRow(word[0]);
            if(k == -1)
                break;
            
            
            string row = keyboard[k];
            //Console.WriteLine(row);
            for(int i=0; i < word.Length;i++){
                if(row.IndexOf(word[i]) == -1){
                    allFound = false;
                    break;
                }
                else
                    allFound = true;
            }
            if(allFound)
                output.Add(w);
            
        }
        
        return output.ToArray<string>();
        
        
    }
    
    public int GetKeyboardRow(char c){
        
        foreach(string row in keyboard){
            if(row.IndexOf(c) != -1)
                return Array.IndexOf(keyboard,row);// keyboard.IndexOf(row);
        }
        
        return -1;
    }

Log in to reply
 

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