LeetCode 500. Keyboard Row

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.
class Solution {
public:
vector<string> findWords(vector<string>& words) {
           map<char,int> checked={{'a',2},{'s',2},{'d',2},{'f',2},{'g',2},{'h',2},{'j',2},{'k',2},{'l',2},
                                  {'q',1},{'w',1},{'e',1},{'r',1},{'t',1},{'y',1},{'u',1},{'i',1},{'o',1},{'p',1},
                                  {'z',3},{'x',3},{'c',3},{'v',3},{'b',3},{'n',3},{'m',3}};
          vector<string> res;
          for(int i=0;i<words.size();i++){
          	  char ch=tolower(words[i][0]);
              int line=checked[ch] , j ;
              for(j=0;j<words[i].size();j++){
                     ch=tolower(words[i][j]);
                  if(checked[ch]!=line)
                     break;
              }
              if(j==words[i].size()) res.push_back(words[i]);
          }
          return res;
}
};
原文地址:https://www.cnblogs.com/A-Little-Nut/p/10067703.html