Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
vector<string> findWords(vector<string>& words)
{
vector<string> res;
for (auto str : words)
{
bool r0 = str.find_first_of("1234567890-=") == string::npos ? false : true;
bool r1 = str.find_first_of("QWERTYUIOPqwertyuiop") == string::npos ? false : true;
bool r2 = str.find_first_of("ASDFGHJKLasdfghjkl") == string::npos ? false : true;
bool r3 = str.find_first_of("ZXCVBNMzxcvbnm") == string::npos ? false : true;
if (r0 + r1 + r2 + r3 == 1)
res.push_back(str);
}
return res;
}
};
int main()
{
Solution Temp;
vector<string>Input;
Input.push_back("123546asd90");
Input.push_back("Hello");
Input.push_back("Alaska");
Input.push_back("Dad");
Input.push_back("Peace");
vector<string>Output;
Output = Temp.findWords(Input);
for (int i = 0; i < Output.size(); i++)
{
cout << Output[i] << endl;
}
}