Keyboard Row

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:

  1. You may use one character in the keyboard more than once.
  2. 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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值