-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathkeyboard-row.py
42 lines (37 loc) · 1.16 KB
/
keyboard-row.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
# Time: O(n)
# Space: O(1)
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
rows = [set(['q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p']),
set(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']),
set(['z', 'x', 'c', 'v', 'b' ,'n', 'm'])]
result = []
for word in words:
k = 0
for i in xrange(len(rows)):
if word[0].lower() in rows[i]:
k = i
break
for c in word:
if c.lower() not in rows[k]:
break
else:
result.append(word)
return result
class Solution2(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
keyboard_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']
single_row_words = []
for word in words:
for row in keyboard_rows:
if all(letter in row for letter in word.lower()):
single_row_words.append(word)
return single_row_words