Skip to content

Commit 5aeb406

Browse files
Add another implementation by regex to No_500
1 parent 228c41a commit 5aeb406

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
import re
22+
from typing import List
23+
class Solution:
24+
25+
26+
def findWords(self, words):
27+
return list( filter(re.compile('(?i)^([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$').match, words) )
28+
29+
30+
31+
# n : the number of word in input list
32+
33+
## Time Complexity: O( n )
34+
#
35+
# The major overhead in time is the for loop iterating on word, which is of O( n )
36+
37+
38+
## Space Complexity: O( n )
39+
#
40+
# The major overhead in space is the storage for output one_row_words, which is of O( n )
41+
42+
43+
def test_bench():
44+
45+
test_data = [
46+
["Hello", "Alaska", "Dad", "Peace"]
47+
]
48+
49+
# expected output:
50+
'''
51+
['Alaska', 'Dad']
52+
'''
53+
54+
for test_words in test_data:
55+
56+
print( Solution().findWords(test_words) )
57+
58+
return
59+
60+
61+
62+
if __name__ == '__main__':
63+
64+
test_bench()
65+

0 commit comments

Comments
 (0)