题目描述:
判断单词的字母是否都在键盘同一行
注意:
str="Hello"
str.lower()是开了一块新空间,并不是把原有的str覆盖,与list的sort()不一样
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
a=['q','w','e','r','t','y','u','i','o','p']
b=['a','s','d','f','g','h','j','k','l']
c=['z','x','c','v','b','n','m']
res=[]
for item in words:
na=0;nb=0;nc=0
for i in range(0,len(item)):
t=item[i].lower()
if t in a:
na+=1;
elif t in b:
nb+=1
elif t in c:
nc+=1
if (na>0 and nb==0 and nc==0) or (na==0 and nb>0 and nc==0) or (na==0 and nb==0 and nc>0):
res.append(item)
return res
if __name__=="__main__":
sol=Solution()
w=["Hello", "Alaska", "Dad", "Peace"]
res=sol.findWords(w)
for item in res:
print res