Easy to understand C++ solution


  • 0
    V

    Instead of using map we can use three strings and find corresponding keys in them to optimize the code.

    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector <string> board, sol;
            bool flag;
            board.push_back("qwertpoiyu");
            board.push_back("asdfglkjh");
            board.push_back("zxcvbmn");
            for(string word : words){
                for(int i=0; i<3; i++){
                    flag = true;                
                    for(char c: word){
                        if(board[i].find(tolower(c)) == -1){
                            flag = false;
                            break;
                        }
                    }
                    if(flag)    sol.push_back(word);
                }
            }
            return sol;
            
        }
        
    };
    

Log in to reply
 

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