[LeetCode]500. Keyboard Row
题目描述
思路
将每一行的字符存入map中,不同行对应不同值,
遍历比较,有不同的就结束该词,全相同则加入result中
代码
class Solution {
public:
vector<string> findWords(vector<string>& words) {
unordered_map<char, int> keyboard;
string line_1 = "qwertyuiopQWERTYUIOP";
string line_2 = "asdfghjklASDFGHJKL";
string line_3 = "zxcvbnmZXCVBNM";
vector<string> result;
for (auto &p : line_1) {
keyboard[p] = 1;
}
for (auto &p : line_2) {
keyboard[p] = 2;
}
for (auto &p : line_3) {
keyboard[p] = 3;
}
for (int i = 0; i < words.size(); ++i){
bool flag = true;
for (int j = 1; j < words[i].size(); ++j){
if (keyboard[words[i][0]] != keyboard[words[i][j]]){
flag = false;
break;
}
}
if (flag){
result.push_back(words[i]);
}
}
return result;
}
};