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
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(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
length = len(words)
memo = []
for iz in range(1,length+1):
memo.append(0)
result = []
str1 = ['Q','W','E','R','Y','T','U','I','O','P','q','w','e','r','t','y','u','i','o','p']
str2 = ['A','S','D','F','G','H','J','K','L','a','s','d','f','g','h','j','k','l']
str3 = ['Z','X','C','V','B','N','M','z','x','c','v','b','n','m']
iy1 = 0
iy3 = 0
iy5 = 0
ix7 = 0
i = 0
for iy in range(1,length+1):
ix1 = 0
for ix in range(1,21):
if str1[ix1] in words[iy1]:
memo[iy1] = memo[iy1] +1
break
ix1 = ix1 +1
iy1 = iy1 + 1
for iy2 in range(1,length+1):
ix3 = 0
print('iy3',iy3)
for ix2 in range(1,19):
if str2[ix3] in words[iy3]:
memo[iy3] = memo[iy3] + 1
print('miy1',memo[iy3])
break
ix3 = ix3 +1
iy3 = iy3 + 1
for iy4 in range(1,length+1):
ix5 = 0
for ix4 in range(1,15):
if str3[ix5] in words[iy5]:
memo[iy5] = memo[iy5] + 1
break
ix5 = ix5 +1
iy5 = iy5 + 1
for ix6 in range(1,length+1):
print('ix7',ix7)
if memo[ix7] ==1:
result.append(words[ix7])
i = i + 1
ix7 = ix7 +1
return result
ps: python中的for循环 for x in range(1,20) 实际只循环19次!!
介绍集合set的操作
http://www.jb51.net/article/57214.htm
s.issubset(t)
s <= t # 测试是否 s 中的每一个元素都在 t 中
def findWords(words):
return [word for row in [set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')] for word in words if set(word.lower()) <= row]