500. 键盘行

给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。

美式键盘 中:

第一行由字符 "qwertyuiop" 组成。
第二行由字符 "asdfghjkl" 组成。
第三行由字符 "zxcvbnm" 组成。


 

示例 1:

输入:words = ["Hello","Alaska","Dad","Peace"]
输出:["Alaska","Dad"]
示例 2:

输入:words = ["omk"]
输出:[]
示例 3:

输入:words = ["adsdf","sfd"]
输出:["adsdf","sfd"]
 

提示:

1 <= words.length <= 20
1 <= words[i].length <= 100
words[i] 由英文字母(小写和大写字母)组成

 1 class Solution(object):
 2     def findWords(self, words):
 3         """
 4         :type words: List[str]
 5         :rtype: List[str]
 6         """
 7 
 8         line1 = set('qwertyuiop')
 9         line2 = set('asdfghjkl')
10         line3 = set('zxcvbnm')
11 
12         res = []
13 
14         for word in words:
15             word = word.encode('utf-8')
16             lower_word = str.lower(word)
17             line = None
18             for i in range(len(lower_word)):
19                 if i == 0:
20                     if lower_word[i] in line1:
21                         line = line1
22                     if lower_word[i] in line2:
23                         line = line2
24                     if lower_word[i] in line3:
25                         line = line3
26                 if lower_word[i] not in line:
27                     break
28                 if i == len(lower_word) - 1:
29                     res.append(word)
30         return res

 

上一篇:LeetCode-每日一题 500. 键盘行 [Java实现] [极速]


下一篇:超过500万海量数据怎样处理