题目描述:
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
本文链接:http://bookshadow.com/weblog/2017/02/05/leetcode-keyboard-row/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。