Solution:
may use a hashmap to label the letters in the same row. Also need to pay attention to the upper or lower letters.
Code:
- class Solution {
- public:
- vector<string> findWords(vector<string>& words) {
- vector<string> res;
- string v1="qwertyuiop", v2="asdfghjkl", v3="zxcvbnm";
- unordered_map<char, int> m;
- for(auto a:v1) m[a]=0;
- for(auto a:v2) m[a]=1;
- for(auto a:v3) m[a]=2;
- for(auto a:words){
- if(a.size()){
- int t = -1;
- if(m.find(a.front())!=m.end()) t = m[a.front()];
- else t = m[(a.front()+32)];
- bool flag = true;
- for(auto b:a){
- if((m.find(b)!=m.end()&&m[b]==t) || (m.find(b+32)!=m.end()&&m[b+32]==t)) continue;
- else{
- flag = false;
- break;
- }
- }
- if(flag) res.push_back(a);
- }
- }
- return res;
- }
- };
No comments :
Post a Comment