Jenifa

Leetcode 500: Keyboard row

May 29th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<locale>
  4.  
  5.  
  6. class solution{
  7. public:
  8.     bool find(std::string word, std::string row)
  9.     {
  10.         for(size_t i = 0; i<word.size(); i++)
  11.         {
  12.             if(row.find(std::tolower(word[i])) == std::string::npos)
  13.             {
  14.                 return false;
  15.             }
  16.         }
  17.         return true;
  18.     }
  19.  
  20.  
  21.     std::vector<std::string>findWords(std::vector<std::string>&words)
  22.     {
  23.         std::string row1 = "qwertyuiop";
  24.  
  25.         std::string row2 = "asdfghjkl";
  26.        
  27.         std::string row3 = "zxcvbnm";
  28.        
  29.         std::vector<std::string>result;
  30.  
  31.         for(std::string & word : words)
  32.         {
  33.             if(find(word, row1) || find(word, row2) || find(word, row3))
  34.             {
  35.                 result.push_back(word);
  36.             }
  37.         }
  38.         return result;
  39.  
  40.     }
  41.  
  42.  
  43. };
  44.  
  45. int main()
  46. {
  47.     solution sol;
  48.  
  49.        std::string word;
  50.  
  51.        std::vector<std::string>temp;
  52.  
  53.        std::vector<std::string>input;
  54.  
  55.        int T;
  56.        
  57.           std::cout<<"how many word do you want to input"<<std::endl;
  58.        
  59.           std::cin>>T;
  60.        
  61.           for(int i=0; i<(T+1); i++)
  62.           {
  63.  
  64.            std::getline(std::cin, word);
  65.  
  66.            input.push_back(word);
  67.  
  68.        }
  69.  
  70.      temp = sol.findWords(input);
  71.  
  72.  
  73.     for(size_t i=0; i<temp.size(); i++)
  74.        {
  75.  
  76.            std::cout<<temp[i]<<std::endl;
  77.  
  78.        }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment