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.

avatag

Example:

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.
class Solution:
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        dic = {'Q':0,'W':0,'E':0,'R':0,'T':0,'Y':0,'U':0,'I':0,'O':0,'P':0,'A':1,'S':1,'D':1,'F':1,'G':1,'H':1,'J':1,'K':1,'L':1,'Z':2,'X':2,'C':2,'V':2,'B':2,'N':2,'M':2}
        res = []
        for word in words:
            tt = word
            word = word.upper()
            temp = dic[word[0]]
            flag = True
            for i in range(1,len(word)):
                if dic[word[i]]!= temp:
                    flag = False
                    break
            if flag:
                res.append(tt)
        return res
原文地址:https://www.cnblogs.com/bernieloveslife/p/9830796.html