# 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 findWords(vector& words) { vector ans; const vector 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; transform(begin(lowerWord), end(lowerWord), begin(lowerWord), ::tolower); const int row = rows[lowerWord[0] - 'a']; const bool isValid = all_of(begin(lowerWord), end(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 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