题目
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.
难度
Easy
方法
利用正则(?i)^(([qwertyuiop]+)|([asdfghjkl]*)|([zxcvbnm]+))$
完全匹配,其中(?i)
表示忽略大小写
python代码
import re
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
result = []
for word in words:
if re.match("(?i)^(([qwertyuiop]+)|([asdfghjkl]*)|([zxcvbnm]+))$", word):
result.append(word)
return result
assert Solution().findWords(["Hello","Alaska","Dad","Peace"]) == ["Alaska","Dad"]
网友评论