Leetcode 500 Solution
This article provides solution to leetcode question 500 (keyboard-row)
Access this page by simply typing in "lcs 500" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/keyboard-row
Solution
class Solution {
public:
vector<string> findWords(vector<string>& words) {
unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
unordered_set<char> row3 {'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
vector<string> res;
for (auto word : words)
{
bool valid = true;
int row = 0;
for (auto ch : word)
{
int next_row = 0;
if (row1.find(tolower(ch)) != row1.end())
next_row = 1;
else if (row2.find(tolower(ch)) != row2.end())
next_row = 2;
else if (row3.find(tolower(ch)) != row3.end())
next_row = 3;
if (row != 0 && row != next_row)
{
valid = false;
break;
}
row = next_row;
}
if (valid)
res.push_back(word);
}
return res;
}
};