C# (Keyboard Row)


  • 0
    A
    public string[] FindWords(string[] words) {
             char[] row1Upper = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P' };
                char[] row2Upper = { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L' };   
                 char[] row3Upper = { 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };
    
                int count = words.Length;
                for (int i=0; i<words.Length; i++)
                {
                   
                    string word = words[i].ToUpper();
                    char[] wordChars = word.ToCharArray();
                    bool lettersInRow1 = false;
                    bool lettersInRow2 = false;
                    bool lettersInRow3 = false;
                    for (int j = 0; j < wordChars.Length; j++)
                    {
                        lettersInRow1 = row1Upper.Contains(wordChars[j]) || lettersInRow1;
                        lettersInRow2 = row2Upper.Contains(wordChars[j]) || lettersInRow2;
                        lettersInRow3 = row3Upper.Contains(wordChars[j]) || lettersInRow3;
                        if (lettersInRow1 && lettersInRow2) { words[i] = ""; count--; break; }
                        if (lettersInRow1 && lettersInRow3) { words[i] = ""; count--; break; }
                        if (lettersInRow2 && lettersInRow3) { words[i] = ""; count--; break; }
                    }               
                }
                 string[] wordsInARow = new string[count];
                 int newIndex = 0;
                for (int k = 0; k< words.Length; k++)
                {
                    if (!String.IsNullOrEmpty(words[k]))
                    {
                        wordsInARow[newIndex] = words[k];
                        newIndex++;
                    }
                }
               return wordsInARow;
        }
    

Log in to reply
 

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