-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
29 lines (29 loc) · 790 Bytes
/
soln.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
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> ans;
vector<string> rows = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
unordered_map<char, int> key;
int row = 0;
for(auto string : rows) {
++row;
for(auto ch : string) {
key[ch] = row;
}
}
unordered_set<int> s;
for(auto string : words) {
s.clear();
bool flag = true;
for(auto ch : string) {
s.insert(key[tolower(ch)]);
if (s.size() > 1) {
flag = false;
break;
}
}
if (flag) ans.push_back(string);
}
return ans;
}
};