close

 

 

 

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:

  1. You may use one character in the keyboard more than once.
  2. 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;
    }
}

 

 

arrow
arrow
    文章標籤
    leetcode
    全站熱搜
    創作者介紹
    創作者 Eric 的頭像
    Eric

    一個小小工程師的心情抒發天地

    Eric 發表在 痞客邦 留言(0) 人氣()