首页 > 学院 > 开发设计 > 正文

Keyboard Row

2019-11-08 01:03:47
字体:
来源:转载
供稿:网友

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"]

Note:

You may use one character in the keyboard more than once.You may assume the input string will only contain letters of alphabet.
class Solution {public:    vector<string> findWords(vector<string>& words) {        string a = "asdfghjkl", b = "qwertyuiop", c = "zxcvbnm";    	vector<string> ans;    	for (vector<string>::iterator it = words.begin(); it < words.end(); ++it)    	{    		string ss = *it;    		string::size_type j = 0;    		int locate = 0;    		while(j < ss.size())    		{    			char ss_char = tolower(ss[j]);    			if ((locate == 0 || locate == 1) && a.find(ss_char) >= 0 && a.find(ss_char) < a.size())    			{    				++j;    				locate = 1;    			}    			else if ((locate == 0 || locate == 2) && b.find(ss_char) >= 0 && b.find(ss_char) < b.size())    			{    				++j;    				locate = 2;    			}    			else if ((locate == 0 || locate == 3) && c.find(ss_char) >= 0 && c.find(ss_char) < c.size())    			{    				++j;    				locate = 3;    			}    			else    				break;    		}    		if (j == ss.size())    			ans.push_back(ss);    	}    	return ans;    }};
class Solution(object):    def findWords(self, words):        """        :type words: List[str]        :rtype: List[str]        """        ans = []        a, b, c = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')        for i in range(len(words)):            letter = set(words[i].lower())            if a & letter == letter or b & letter == letter or c & letter == letter:                ans.append(words[i])        return ans                
class Solution {public:    vector<string> findWords(vector<string>& words) {        unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};        unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};         unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};        vector<unordered_set<char>> rows {row1, row2, row3};                        vector<string> validWords;        for(int i=0; i<words.size(); ++i){            int row=0;                        for(int k=0; k<3; ++k){                if(rows[k].count((char)tolower(words[i][0])) > 0) row = k;            }                        validWords.push_back(words[i]);            for(int j=1; j<words[i].size(); ++j){                if(rows[row].count((char)tolower(words[i][j])) == 0){                    validWords.pop_back();                    break;                }            }                    }        return validWords;    }};
def findWords(self, words):    line1, line2, line3 = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')    ret = []    for word in words:      w = set(word.lower())      if w.issubset(line1) or w.issubset(line2) or w.issubset(line3):        ret.append(word)    return ret

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表