MY CPP Solution with map


  • 0
    B
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string layout = "qwertyuiopQWERTYUIOP asdfghjklASDFGHJKL zxcvbnmZXCVBNM";
            map<char, int> row_map;
            int idx=0;
            for(int i=0;i<layout.size();i++){
                if(layout[i]==' ')idx++;
                else
                row_map[layout[i]]=idx;
            }
            vector<string> result;
            for(int i=0;i<words.size();i++){
                string word = words[i];
                int row_num = row_map[word[0]];
                
                bool flag=true;
                for(int j=1;j<word.size();j++){
                    if(row_map[word[j]]!=row_num){
                        flag=false;
                        break;
                    }
                }
                if(flag)result.push_back(word);
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.