给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
美式键盘 中:
- 第一行由字符 “qwertyuiop” 组成。
- 第二行由字符 “asdfghjkl” 组成。
- 第三行由字符 “zxcvbnm” 组成。
class Solution:
def findWords(self, words: List[str]) -> List[str]:
ans = []
rowIdx = "12210111011122000010020202"
for word in words:
idx = rowIdx[ord(word[0].lower()) - ord('a')]
if all(rowIdx[ord(ch.lower()) - ord('a')] == idx for ch in word):
ans.append(word)
return ans