C++ solution


  • 0
    J
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector<string> result;
            vector<string> tmp = words;
            for(int i = 0; i < words.size(); i++)
            {
                bool flag = true;
                bool flag1 = false, flag2 = false, flag3 = false;
                transform(tmp[i].begin(), tmp[i].end(), tmp[i].begin(), ::tolower);  
                if(tmp[i].length() <= 1)
                    result.push_back(words[i]);
                else
                {
                    for(char ch : tmp[i])
                    {
                        if(ch == 'q' || ch == 'w' || ch == 'e' || ch == 'r' || ch == 't' || ch == 'y' 
                        || ch == 'u' || ch == 'i' || ch == 'o' || ch == 'p')
                        {
                            if(flag2 == false && flag3 == false)
                                flag1 = true;
                            else
                            {
                                flag = false;
                                break;
                            }
                        }
                        else if(ch == 'a' || ch == 's' || ch == 'd' || ch == 'f' || ch == 'g' || ch == 'h' 
                             || ch == 'j' || ch == 'k' || ch == 'l')
                        {
                            if(flag1 == false && flag3 == false)
                                flag2 = true;
                            else
                            {
                                flag = false;
                                break;
                            }               
                        }
                        else if(ch == 'z' || ch == 'x' || ch == 'c' || ch == 'v' || ch == 'b' || ch == 'n' || ch == 'm')
                        {
                            if(flag1 == false && flag2 == false)
                                flag3 = true;
                            else
                            {
                                flag = false;
                                break;
                            }
                        }
                    }
                    if(flag)
                        result.push_back(words[i]);
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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