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.

Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
解题思路:这题涉及到集合运算,运用python是最方便的。
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
rs = map(set, ['qwertyuiop','asdfghjkl','zxcvbnm'])
ans = []
for word in words:
wset = set(word.lower())
if any(wset <= rset for rset in rs):
ans.append(word)
return ans
本文介绍了一种算法,用于从给定的单词列表中筛选出仅使用美国键盘单行字母即可拼写的单词。通过集合运算的方式,实现了高效的筛选过程。
352

被折叠的 条评论
为什么被折叠?



