Skip to content

Latest commit

 

History

History
86 lines (37 loc) · 1.09 KB

0500._Keyboard_Row.md

File metadata and controls

86 lines (37 loc) · 1.09 KB

500. Keyboard Row

难度: Easy

刷题内容

原题连接

内容描述

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:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

解题方案

思路 1 - 时间复杂度: O(sum(len(word) for word in words))- 空间复杂度: O(1)******

beats 46.75%

class Solution:
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        lookup = {}
        for c in 'qwertyuiop':
            lookup[c] = 1
        for c in 'asdfghjkl':
            lookup[c] = 2
        for c in 'zxcvbnm':
            lookup[c] = 3
            
        res = []
        for word in words:
            first = word[0].lower()
            if all(lookup[c.lower()] == lookup[first] for c in word):
                res.append(word)
        return res