-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_row.cpp
40 lines (37 loc) · 1002 Bytes
/
keyboard_row.cpp
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
#include <iostream>
#include <map>
#include <cctype>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> rows = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
map<char, int> row_map;
for (int i=0; i < 3; i++) {
for (auto ch: rows[i]) {
row_map[ch] = i;
}
}
vector<string> ans;
for (auto &word: words) {
int row = row_map[toupper(word[0])];
bool push = true;
for (auto ch: word) {
if (row != row_map[toupper(ch)]) {
push = false;
break;
}
}
if (push)
ans.push_back(word);
}
return ans;
}
};
int main() {
Solution s;
vector<string> words = {"Hello", "Alaska", "Dad", "Peace"};
for (auto &v: s.findWords(words))
cout << v << '\n';
}