-
Notifications
You must be signed in to change notification settings - Fork 28
/
Keyboard_Row.cpp
31 lines (30 loc) · 968 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
class Solution {
public:
vector<string> findWords(vector<string>& words) {
unordered_map<int, int> keyRow;
string firstRow = "QWERTYUIOP";
for(int i = 0; i < firstRow.length(); i++) {
keyRow[firstRow[i]] = 1;
}
string secondRow = "ASDFGHJKL";
for(int i = 0; i < secondRow.length(); i++) {
keyRow[secondRow[i]] = 2;
}
string thirdRow = "ZXCVBNM";
for(int i = 0; i < thirdRow.length(); i++) {
keyRow[thirdRow[i]] = 3;
}
vector<string> result;
for(int i = 0; i < words.size(); i++) {
string word = words[i];
unordered_set<int> rows;
for(int j = 0; j < (int)word.length(); j++) {
rows.insert(keyRow[toupper(word[j])]);
}
if(rows.size() == 1) {
result.push_back(word);
}
}
return result;
}
};