-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0500.keyboard-row.cpp
More file actions
41 lines (32 loc) · 928 Bytes
/
0500.keyboard-row.cpp
File metadata and controls
41 lines (32 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// https://leetcode.com/problems/keyboard-row/
class Solution {
private:
vector<string> rows = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
int findRow(char c) {
for (int i = 0; i < rows.size(); i++) {
if (rows[i].contains(c)) {
return i;
}
}
return -1;
}
public:
vector<string> findWords(vector<string>& words) {
vector<string> result;
for (string word : words) {
int rowIndex = findRow(tolower(word[0]));
bool isSingleRow = true;
for (int i = 1; i < word.size(); i++) {
char c = tolower(word[i]);
if (!rows[rowIndex].contains(c)) {
isSingleRow = false;
break;
}
}
if (isSingleRow) {
result.push_back(word);
}
}
return result;
}
};