Skip to content

500. Keyboard Row

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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,
                           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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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,
                        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]);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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