500. Keyboard Row LeetCode Solution

In this guide, you will get 500. Keyboard Row LeetCode Solution with the best time and space complexity. The solution to Keyboard Row problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Keyboard Row solution in C++
  4. Keyboard Row solution in Java
  5. Keyboard Row solution in Python
  6. Additional Resources
500. Keyboard RowLeetCode Solution image

Problem Statement of Keyboard Row

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.
In the American keyboard:

the first row consists of the characters “qwertyuiop”,
the second row consists of the characters “asdfghjkl”, and
the third row consists of the characters “zxcvbnm”.

Example 1:

Input: words = [“Hello”,”Alaska”,”Dad”,”Peace”]
Output: [“Alaska”,”Dad”]
Explanation:
Both “a” and “A” are in the 2nd row of the American keyboard due to case insensitivity.

Example 2:

Input: words = [“omk”]
Output: []

Example 3:

Input: words = [“adsdf”,”sfd”]
Output: [“adsdf”,”sfd”]

Constraints:

1 <= words.length <= 20
1 <= words[i].length <= 100
words[i] consists of English letters (both lowercase and uppercase).

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

500. Keyboard Row LeetCode Solution in C++

class Solution {
 public:
  vector<string> findWords(vector<string>& words) {
    vector<string> ans;
    const vector<int> rows{2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3,
, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3};

    for (const string& word : words) {
      string lowerWord = word;
      ranges::transform(lowerWord, lowerWord.begin(), ::tolower);
      const int row = rows[lowerWord[0] - 'a'];
      const bool isValid = ranges::all_of(
          lowerWord, [&](int c) { return rows[c - 'a'] == row; });
      if (isValid)
        ans.push_back(word);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

500. Keyboard Row LeetCode Solution in Java

class Solution {
  public String[] findWords(String[] words) {
    List<String> ans = new ArrayList<>();
    final int[] rows = {2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3,
, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3};

    for (final String word : words) {
      final String lowerWord = word.toLowerCase();
      final int row = rows[lowerWord.charAt(0) - 'a'];
      final boolean isValid = lowerWord.chars().allMatch(c -> rows[c - 'a'] == row);
      if (isValid)
        ans.add(word);
    }

    return ans.toArray(new String[0]);
  }
}
// code provided by PROGIEZ

500. Keyboard Row LeetCode Solution in Python

class Solution:
  def findWords(self, words: list[str]) -> list[str]:
    ans = []
    rows = [set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')]

    for word in words:
      lowerWord = set(word.lower())
      if any(lowerWord <= row for row in rows):
        ans.append(word)

    return ans
# code by PROGIEZ

Additional Resources

See also  225. Implement Stack using Queues LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.