美文网首页
[LeetCode]500. Keyboard Row

[LeetCode]500. Keyboard Row

作者: Eazow | 来源:发表于2017-05-05 11:55 被阅读35次
题目

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:

  1. You may use one character in the keyboard more than once.
  2. 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"]

相关文章

网友评论

      本文标题:[LeetCode]500. Keyboard Row

      本文链接:https://www.haomeiwen.com/subject/nhwjtxtx.html