Skip to content

Commit 228c41a

Browse files
Add No_500: Keyboard Row
1 parent f28bf45 commit 228c41a

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
3+
Description:
4+
5+
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.
6+
7+
Example:
8+
9+
Input: ["Hello", "Alaska", "Dad", "Peace"]
10+
Output: ["Alaska", "Dad"]
11+
12+
13+
Note:
14+
15+
You may use one character in the keyboard more than once.
16+
You may assume the input string will only contain letters of alphabet.
17+
18+
'''
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'''
2+
3+
Description:
4+
5+
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.
6+
7+
Example:
8+
9+
Input: ["Hello", "Alaska", "Dad", "Peace"]
10+
Output: ["Alaska", "Dad"]
11+
12+
13+
Note:
14+
15+
You may use one character in the keyboard more than once.
16+
You may assume the input string will only contain letters of alphabet.
17+
18+
'''
19+
20+
21+
22+
from typing import List
23+
class Solution:
24+
25+
26+
def findWords(self, words: List[str]) -> List[str]:
27+
28+
29+
group = [ 'qwertyuiop', 'asdfghjkl', 'zxcvbnm' ]
30+
one_row_words = []
31+
32+
for word in words:
33+
34+
word_low = word.lower()
35+
36+
flag = all( ch in group[0] for ch in word_low ) or \
37+
all( ch in group[1] for ch in word_low ) or \
38+
all( ch in group[2] for ch in word_low )
39+
40+
if flag:
41+
one_row_words.append(word)
42+
43+
return one_row_words
44+
45+
46+
47+
# n : the number of word in input list
48+
49+
## Time Complexity: O( n )
50+
#
51+
# The major overhead in time is the for loop iterating on word, which is of O( n )
52+
53+
54+
## Space Complexity: O( n )
55+
#
56+
# The major overhead in space is the storage for output one_row_words, which is of O( n )
57+
58+
59+
def test_bench():
60+
61+
test_data = [
62+
["Hello", "Alaska", "Dad", "Peace"]
63+
]
64+
65+
# expected output:
66+
'''
67+
['Alaska', 'Dad']
68+
'''
69+
70+
for test_words in test_data:
71+
72+
print( Solution().findWords(test_words) )
73+
74+
return
75+
76+
77+
78+
if __name__ == '__main__':
79+
80+
test_bench()
81+

0 commit comments

Comments
 (0)