Please review my accepted solution!! How can I improve on this?


  • 0
    A

    Though the solution is accepted, I need feedback to improve my skills.

    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string a ="qwertyuiop";
            string b ="asdfghjkl";
            string c ="zxcvbnm";
            vector<string>A ={a,b,c};
            vector<string>ans;
            int flagA=1,flagB=1,flagC=1;
            
            for(int i=0;i<words.size();i++)
            {
                string temp= (words[i]);
                transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
                
                flagA=a.find(temp.at(0));
                flagB=b.find(temp.at(0));
                flagC=c.find(temp.at(0));
                
                int pos;
                if(flagA>=0) pos=0;
                else if(flagB>=0) pos=1;
                else if(flagC>=0)pos=2;
                int counter=0;
                
                for(int j=0;j<temp.length();j++)
                {
                    int ret = A[pos].find(temp.at(j));
                    
                    if(ret>=0 && ret<A[pos].length())
                    {
                        counter++;
                    }
                    else
                    {
                        break;
                    }
                    
                    if(counter==temp.length())
                    {
                        ans.push_back(words[i]);
                    }
                }
                    
                
            }
            return ans;
        }
    };

Log in to reply
 

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