LeetCode Solutions
500. Keyboard Row
Time: $O(n)$ Space: $O(n)$
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;
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;
}
};
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]);
}
}
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