500. Keyboard Row

Description:

https://leetcode.com/problems/keyboard-row/#/description

Boundary:

NO

Algorithm:

Not difficult. Use:

str.find() < str.nops

Code:

class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> result;
for (int k = 0; k < words.size(); k++)
{
if (line1_Big.find(words[k][0]) < line1_Big.size() || line1_Sml.find(words[k][0]) < line1_Sml.size())
{
if (ifExist(words[k], line1_Big, line1_Sml))
result.push_back(words[k]);
}
else if (line2_Big.find(words[k][0]) < line2_Big.size() || line2_Sml.find(words[k][0]) < line2_Sml.size())
{
if (ifExist(words[k], line2_Big, line2_Sml))
result.push_back(words[k]);
}
else
{
if (ifExist(words[k], line3_Big, line3_Sml))
result.push_back(words[k]);
}

}
return result;
}
private:
string line1_Big = “QWERTYUIOP”;
string line1_Sml = “qwertyuiop”;

string line2_Big = “ASDFGHJKL”;
string line2_Sml = “asdfghjkl”;

string line3_Big = “ZXCVBNM”;
string line3_Sml = “zxcvbnm”;
bool ifExist(string word, string big, string small)
{
for (int i = 1; i < word.size(); i++)
{
if (big.find(word[i]) > big.size() && small.find(word[i]) > small.size())
return false;
}
return true;
}
};

Leave a Reply

Your email address will not be published. Required fields are marked *