Keyboard Row

2017/12/6 posted in  leetcode

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        set<char> row1 = {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
        set<char> row2 = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
        set<char> row3 = { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
        vector<set<char>> rows = {row1,row2,row3};
        vector<string> res;
        for (auto str: words) {
            int log = 0;
            for (int i = 0; i<rows.size(); i++) {
                if (rows[i].count((char)tolower(str[0]))>0) {
                    log = i;
                }
            }
            for (int j = 0; j<str.length(); j++) {
                if (rows[log].count((char)tolower(str[j]))==0) {
                    break;
                }
                if (j==str.length()-1) {
                    res.push_back(str);
                }
            }
        }
        return res;
    }
};