Jenifa

Leetcode 500: Keyboard row

May 25th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5.  
  6.  
  7. class solution{
  8.  
  9.  
  10. public:
  11.  
  12.     std::vector<std::string>findWords(std::vector<std::string>& words)
  13.     {
  14.  
  15.         char row1[] ={'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
  16.  
  17.         char row2[] ={'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
  18.  
  19.         char row3[]={'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
  20.  
  21.         bool result[words.size()+1];
  22.  
  23.  
  24.  
  25.         // iterate through the words
  26.  
  27.         for(size_t i =0; i<words.size(); i++)
  28.         {
  29.             result[i] = true;
  30.  
  31.             char *found;
  32.  
  33.  
  34.  
  35.             // search through row1
  36.  
  37.             found = std::find(row1, row1+20, words[i][0]); //if first letter is found in row 1
  38.  
  39.             if(found != row1+20)
  40.             {
  41.  
  42.                 for(size_t j = 1; j<words[i].size(); j++) //iterate to check if others are there too
  43.                 {
  44.  
  45.                     found = std::find(row1, row1+20, words[i][j]);
  46.  
  47.                     if(found == row1+20) //
  48.                     {
  49.  
  50.                         result[i] = false;
  51.  
  52.                         break;
  53.                     }
  54.                 }
  55.             }
  56.  
  57.  
  58.  
  59.             // search through row2
  60.  
  61.             found = std::find(row2, row2+18, words[i][0]);
  62.  
  63.             if(found != row2+18)
  64.             {
  65.  
  66.                 for(size_t j = 1; j<words[i].size(); j++)
  67.                 {
  68.  
  69.                     found = std::find(row2, row2+18, words[i][j]);
  70.  
  71.                     if(found == row2+18)
  72.                     {
  73.  
  74.                         result[i] = false;
  75.  
  76.                         break;
  77.  
  78.                     }
  79.                 }
  80.             }
  81.  
  82.  
  83.  
  84.  
  85.             // search through row3
  86.  
  87.             found = std::find(row3, row3+14, words[i][0]);
  88.  
  89.             if(found != row3+14)
  90.             {
  91.  
  92.                 for(size_t j = 1; j<words[i].size(); j++)
  93.                 {
  94.  
  95.                     found = std::find(row3, row3+14, words[i][j]);
  96.  
  97.                     if(found == row3+14)
  98.                     {
  99.  
  100.                         result[i] = false;
  101.  
  102.                         break;
  103.  
  104.                     }
  105.                 }
  106.             }
  107.  
  108.  
  109.         }
  110.  
  111.         std::vector<std::string>temp;
  112.  
  113.         for(size_t i =0; i<words.size(); i++)
  114.         {
  115.  
  116.             if(result[i])
  117.             {
  118.  
  119.                 temp.push_back(words[i]);
  120.  
  121.             }
  122.  
  123.         }
  124.  
  125.         return temp;
  126.    }
  127.  
  128. };
  129.  
  130. int main()
  131. {
  132.  
  133.  
  134. solution sol;
  135.  
  136.    std::string word;
  137.  
  138.    std::vector<std::string>temp;
  139.  
  140.    std::vector<std::string>input;
  141.  
  142.    int T;
  143.  
  144.    std::cout<<"how many word do you want to input"<<std::endl;
  145.  
  146.    std::cin>>T;
  147.  
  148.    for(int i=0; i<(T+1); i++)
  149.    {
  150.  
  151.        std::getline(std::cin, word);
  152.  
  153.        input.push_back(word);
  154.  
  155.    }
  156.  
  157.  temp = sol.findWords(input);
  158.  
  159.  
  160. for(size_t i=0; i<temp.size(); i++)
  161.    {
  162.  
  163.        std::cout<<temp[i]<<std::endl;
  164.  
  165.    }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment