[LeetCode]500. Keyboard Row

题目

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.

[LeetCode]500. Keyboard Row_第1张图片

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)