-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem_0500.py
30 lines (27 loc) · 972 Bytes
/
problem_0500.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
"""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.
"""
class Solution:
def __init__(self):
self.lines = [
{"e", "u", "y", "o", "r", "t", "w", "q", "p", "i"},
{"g", "d", "s", "k", "f", "l", "a", "j", "h"},
{"z", "m", "c", "b", "x", "n", "v"},
]
def findWords(self, words):
res = []
for word in words:
is_valid = True
this_line = None
for ch in word:
for i in range(3):
if ch in self.lines[i]:
if this_line is not None:
if this_line != i:
is_valid = False
break
this_line = i
continue
if is_valid:
res.append(word)
return res