The Keyboard Row solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Keyboard Row
C++, Python and TypeScript solutions for Keyboard Row.
Solution files
Python•keyboard-row/synced-solution.py
def findWords(words):
rows = [
set('qwertyuiop'),
set('asdfghjkl'),
set('zxcvbnm')
]
result = []
for word in words:
word_lower = word.lower()
for row in rows:
if all(char in row for char in word_lower):
result.append(word)
break
return result
C++•keyboard-row/synced-solution.cpp
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<set<char>> rows = {
set<char>{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'},
set<char>{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
set<char>{'z', 'x', 'c', 'v', 'b', 'n', 'm'}
};
vector<string> result;
for (const string& word : words) {
for (const auto& row : rows) {
bool found = true;
for (char c : word) {
if (row.find(tolower(c)) == row.end()) {
found = false;
break;
}
}
if (found) {
result.push_back(word);
break;
}
}
}
return result;
}
};
TypeScript•keyboard-row/synced-solution.ts
function findWords(words: string[]): string[] {
const rows = [
new Set('qwertyuiop'),
new Set('asdfghjkl'),
new Set('zxcvbnm')
];
const result: string[] = [];
for (const word of words) {
const wordLower = word.toLowerCase();
for (const row of rows) {
if ([...wordLower].every(char => row.has(char))) {
result.push(word);
break;
}
}
}
return result;
}