Leetcode #500 Keyboard Row

来源:互联网 发布:网络漫画培训 编辑:程序博客网 时间:2024/05/01 20:34

Description

Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard.

Note

  • You may use one character in the keyboard more than once.
  • You may assume the input string will only contain letters of alphabet.

Example

Input: [“Hello”, “Alaska”, “Dad”, “Peace”]
Output: [“Alaska”, “Dad”]

Explain

单词各字母都在键盘上同一行则输出该单词

Code

class Solution(object):    def findWords(self, words):        """        :type words: List[str]        :rtype: List[str]        """        top = ['q','Q','w','W','e','E','r','R','t','T','y','Y','u','U','i','I','o','O','p','P']        mid = ['a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L']        buttom = ['z','Z','x','X','c','C','v','V','b','B','n','N','m','M']        res = []        for item in words:            a = b = c = 0            for i in item:                if i in top:                    a = 1                if i in mid:                    b = 1                if i in buttom:                    c = 1            if a + b + c == 1:                res.append(item)        return res
0 0
原创粉丝点击