7/21 每日一題(檢查字串是否可以由鍵盤上的其中一行鍵入)
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"]
Constraints:
1 <= words.length <= 201 <= words[i].length <= 100words[i]consists of English letters (both lowercase and uppercase).
class Solution:
def findWords(self, words: List[str]) -> List[str]:
first=set('qwertyuiop')
second=set('asdfghjkl')
third=set('zxcvbnm')
res=[]
for word in words:
w=set(word.lower())
#集合跟集合比較,另外避免大小寫產生錯誤
if w <= first or w <= second or w <= third:
#如果他符合這三行按鍵的子集合就家道list中
res.append(word)
return res
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁