难度: 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