Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<string>
- #include<vector>
- #include<algorithm>
- class solution{
- public:
- std::vector<std::string>findWords(std::vector<std::string>& words)
- {
- char row1[] ={'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
- char row2[] ={'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
- char row3[]={'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
- bool result[words.size()+1];
- // iterate through the words
- for(size_t i =0; i<words.size(); i++)
- {
- result[i] = true;
- char *found;
- // search through row1
- found = std::find(row1, row1+20, words[i][0]); //if first letter is found in row 1
- if(found != row1+20)
- {
- for(size_t j = 1; j<words[i].size(); j++) //iterate to check if others are there too
- {
- found = std::find(row1, row1+20, words[i][j]);
- if(found == row1+20) //
- {
- result[i] = false;
- break;
- }
- }
- }
- // search through row2
- found = std::find(row2, row2+18, words[i][0]);
- if(found != row2+18)
- {
- for(size_t j = 1; j<words[i].size(); j++)
- {
- found = std::find(row2, row2+18, words[i][j]);
- if(found == row2+18)
- {
- result[i] = false;
- break;
- }
- }
- }
- // search through row3
- found = std::find(row3, row3+14, words[i][0]);
- if(found != row3+14)
- {
- for(size_t j = 1; j<words[i].size(); j++)
- {
- found = std::find(row3, row3+14, words[i][j]);
- if(found == row3+14)
- {
- result[i] = false;
- break;
- }
- }
- }
- }
- std::vector<std::string>temp;
- for(size_t i =0; i<words.size(); i++)
- {
- if(result[i])
- {
- temp.push_back(words[i]);
- }
- }
- return temp;
- }
- };
- int main()
- {
- solution sol;
- std::string word;
- std::vector<std::string>temp;
- std::vector<std::string>input;
- int T;
- std::cout<<"how many word do you want to input"<<std::endl;
- std::cin>>T;
- for(int i=0; i<(T+1); i++)
- {
- std::getline(std::cin, word);
- input.push_back(word);
- }
- temp = sol.findWords(input);
- for(size_t i=0; i<temp.size(); i++)
- {
- std::cout<<temp[i]<<std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment