Very easy understanding and naive solution also 0ms


  • 0
    S
    vector<string> findWords(vector<string>& words) {
        unordered_map<char,int> m;
        m['q'] = 1;
        m['w'] = 1;
        m['e'] = 1;
        m['r'] = 1;
        m['t'] = 1;
        m['y'] = 1;
        m['u'] = 1;
        m['i'] = 1;
        m['o'] = 1;
        m['p'] = 1;
        
        m['a'] = 2;
        m['s'] = 2;
        m['d'] = 2;
        m['f'] = 2;
        m['g'] = 2;
        m['h'] = 2;
        m['j'] = 2;
        m['k'] = 2;
        m['l'] = 2;
        
         m['z'] = 3;
         m['x'] = 3;
         m['c'] = 3;
         m['v'] = 3;
         m['b'] = 3;
         m['n'] = 3;
         m['m'] = 3;
        
        vector<string> res;
        for(string s:words)
        {
            int row = m[(char)tolower(s[0])];
            bool flag = true;
            for(char c:s)
            {
                if(m[(char)tolower(c)] != row)
                {
                    flag =false;
                    break;
                }
            }
            if(flag==true) res.push_back(s);
        }
        return res;
    }

Log in to reply
 

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