[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.


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"]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,632评论 0 23
  • 想堕落又不能堕落,一个人扛很累。
    szg暴风暴风阅读 1,607评论 0 0
  • 我还是好想念你 我还是那么的崇拜你 我还是觉得在我的朋友圈里你最棒 我要追寻你 我也要活成你的样子 因为我爱你的那个样子
    AABM阅读 1,679评论 0 0
  • 1字符串 字符串简而言之就是一串字母,用双引号引用起来,你可以给上次的情人向量的每一年的情人加上字符串。比如"ki...
    叮宕阅读 2,939评论 0 0
  • 我知道我将留你不住 看着你离开前的征兆 心中很是焦虑,担心 这是我作为母亲唯一能做的了 我也不知还能做什么 我想知...
    疯疯之内阅读 1,081评论 0 0

友情链接更多精彩内容