[LeetCode]#500. Keyboard Row
Environment: Python 3.8
Key technique: set, subset ≤
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"]
Analysis:
- There are three set{} which are {‘p’, ‘i’, ‘o’, ‘u’, ‘q’, ‘w’, ‘t’, ‘y’, ‘e’, ‘r’}, {‘s’, ‘f’, ‘j’, ‘d’, ‘k’, ‘l’, ‘g’, ‘h’, ‘a’}, {‘m’, ‘b’, ‘c’, ‘v’, ‘x’, ’n’, ‘z’}
- If input set is subset of step1 set. Append it to ans[]
- Return ans.
Solution:
class Solution:
def findWords(self, words):
ans=[]
set1=set("qwertyuiop")
set2=set("asdfghjkl")
set3=set("zxcvbnm")
for i in words:
if set(i.lower()) <= set1:
ans.append(i)
elif set(i.lower()) <= set2:
ans.append(i)
elif set(i.lower()) <= set3:
ans.append(i)return ans
Submissions:
