Keyboard Row

来源:互联网 发布:淘宝图片版权保护 编辑:程序博客网 时间:2024/05/17 03:33

Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

American keyboard

Example 1:
Input: [“Hello”, “Alaska”, “Dad”, “Peace”]
Output: [“Alaska”, “Dad”]

class Solution {public:    vector<string> findWords(vector<string>& words) {        string book[3]={"qwertyuiop","asdfghjkl","zxcvbnm"};        vector<int> vt(26,0);        for(int i = 0; i < 3; ++i){            for(auto& ch:book[i]){                vt[ch-'a']=i;            }        }        vector<string> res;        for(int i= 0;i<words.size();++i){            string temp = words[i];            char temp_ch = temp[0];            if(temp_ch>='A'&&temp_ch<='Z')                    temp_ch+=32;            int flag = vt[temp_ch-'a'];            bool valid = true;            for(auto ch:temp){                if(ch>='A'&&ch<='Z')                    ch+=32;                if(vt[ch-'a']!=flag){                    valid = false;                    break;                }            }            if(valid)                res.push_back(temp);        }        return res;    }};
0 0
原创粉丝点击