-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindWords.py
56 lines (51 loc) · 1.67 KB
/
findWords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# _*_ coding = utf-8 _*_
'''
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:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
'''
"""
:type words: List[str]
:rtype: List[str]
"""
class Solution(object):
def findWords(self, words):
row1 = "qwertyuiopQWERTYUIOP"
row2 = "asdfghjklASDFGHJKL"
row3 = "zxcvbnmZXCVBNM"
out = []
for word in words:
if word[0] in row1:
i , flag = 0 , 1
while i<len(word) and flag:
if word[i] not in row1:
flag = 0
i += 1
if i == len(word) and flag:
out.append(word)
elif word[0] in row2:
i , flag = 0 , 1
while i<len(word) and flag:
if word[i] not in row2:
flag = 0
i += 1
if i == len(word) and flag:
out.append(word)
elif word[0] in row3:
i , flag = 0 , 1
while i<len(word) and flag:
if word[i] not in row3:
flag = 0
i += 1
if i == len(word) and flag:
out.append(word)
return out
if __name__ == "__main__":
sol = Solution()
words = ["abdfs","cccd","a","qwwewm"]
print(sol.findWords(words))