-
Notifications
You must be signed in to change notification settings - Fork 0
/
500. Keyboard Row.py
43 lines (31 loc) · 1.13 KB
/
500. 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
42
43
"""
https://leetcode.com/problems/keyboard-row/submissions/
Given an array of strings words, return the words that can be typed using letters of the alphabet
on only one row of American keyboard like the image below.
In the American keyboard:
the first row consists of the characters "qwertyuiop",
the second row consists of the characters "asdfghjkl", and
the third row consists of the characters "zxcvbnm".
Example 1:
Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
Example 2:
Input: words = ["omk"]
Output: []
Example 3:
Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]
"""
class Solution(object):
def findWords(self, words):
rows = (set("qwertyuiop"), set("asdfghjkl"), set("zxcvbnm"))
res = []
for word in words:
for row in rows:
if set(word.lower()).issubset(row):
res.append(word)
return res
solution = Solution()
assert solution.findWords(["Hello", "Alaska", "Dad", "Peace"]) == ["Alaska", "Dad"]
assert solution.findWords(["omk"]) == []
assert solution.findWords(["adsdf", "sfd"]) == ["adsdf", "sfd"]