题目描述:
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
示例1:
输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"]
注意:
- 你可以重复使用键盘上同一字符。
- 你可以假设输入的字符串将只包含字母。
代码:
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
list1=["q","w","r","t","y","u","i","o","p","e"]
list2=["a","s","d","f","g","h","j","k","l"]
list3=["z","x","c","v","b","n","m",]
listout=[]
for str1 in words:
temp = 1
#str1 = str1.lower
if(str1.lower()[0] in list1):
list4 = list1
elif(str1.lower()[0] in list2):
list4 = list2
else:
list4 = list3
for i in range(1,len(str1)):
if(not (str1.lower()[i] in list4)):
temp = 0
break
if(temp):
listout.append(str1)
return listout