zoukankan      html  css  js  c++  java
  • 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. 

    American keyboard

    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.

    Solution 1: use unordered_map, if each char's value in a word equal the value of the first char, the word can be typed using the letters in the same row.

     1 class Solution {
     2 public:
     3     vector<string> findWords(vector<string>& words) {
     4         unordered_map<char,int> m={{'Q',0},{'W',0},{'E',0},{'R',0},{'T',0},{'Y',0},{'U',0},{'I',0},{'O',0},{'P',0},
     5                                    {'A',1},{'S',1},{'D',1},{'F',1},{'G',1},{'H',1},{'J',1},{'K',1},{'L',1},
     6                                    {'Z',2},{'X',2},{'C',2},{'V',2},{'B',2},{'N',2},{'M',2}};
     7         vector<string> res;
     8         for (string word: words){
     9             int i;
    10             char firstletter=word[0]<='z'&&word[0]>='a'?word[0]-32:word[0];
    11             for (i=1;i<word.length();i++){
    12                 if(m[word[i]<='z'&&word[i]>='a'?word[i]-32:word[i]]!=m[firstletter]){
    13                     break;
    14                 }
    15             }
    16             if (i==word.length()){
    17                 res.push_back(word);
    18             }
    19         }
    20         return res;
    21     }
    22 };

    Solution 2: use unordered_set

     1 class Solution {
     2 public:
     3     vector<string> findWords(vector<string>& words) {
     4         unordered_set<char> row1={'Q','W','E','R','T','Y','U','I','O','P'};
     5         unordered_set<char> row2={'A','S','D','F','G','H','J','K','L'};
     6         unordered_set<char> row3={'Z','X','C','V','B','N','M'};
     7         vector<string> res;
     8         for(string word:words){
     9             int one=0,two=0,three=0;
    10             for(char c:word){
    11                 c=(c>='a')?c-32:c;
    12                 if (row1.count(c)) one = 1;//can't use one=row1.count(c),because we need to save the previous value of one
    13                 if (row2.count(c)) two = 1;
    14                 if (row3.count(c)) three = 1;
    15                 if (one+two+three>1) break;
    16             }
    17             if (one+two+three==1) res.push_back(word);
    18         }
    19         return res;
    20     }
    21 };
  • 相关阅读:
    201521123038 《Java程序设计》 第五周学习总结
    201521123020 《Java程序设计》第4周学习总结
    201521123020 《Java程序设计》第3周学习总结
    201521123020《Java程序设计》第2周学习总结
    Java第十二周学习总结
    Java第十一周学习总结
    Java第十周学习总结
    Java第九周学习总结
    Java第八周学习总结
    Java第七周学习总结
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6805017.html
Copyright © 2011-2022 走看看