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) { str[0]="qwertyuiop",str[1]="asdfghjkl",str[2]="zxcvbnm"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < str[i].size(); ++j) { hash[i][str[i][j]]=true; } } vector<string> res; for (int i = 0; i < words.size(); ++i) { if(words[i].size()==1) res.push_back(words[i]); else{ int j=1; for ( ; j < words[i].size(); ++j) { if(find_char(tolower(words[i][j]))!=find_char(tolower(words[i][j-1]))) break; } if(j==words[i].size()) res.push_back(words[i]); } } return res; } int find_char(char ch){ int hash_start_idx=0; for (int hash_idx = 0; hash_idx < 3; ++hash_idx) { if(hash[hash_idx].find(ch)!=hash[hash_idx].end()) { hash_start_idx=hash_idx; break; } } return hash_start_idx; } private: unordered_map<char,bool> hash[3]; string str[3]; };