[
string
hash table
]
Leetcode 0500 Keyboard Row
Problem statement
https://leetcode.com/problems/keyboard-row/
Solution
Note, letters can be capital and can be not capital. Create hash table with 26
elements, with correspondences between letters and row. Then just traverse each word, apply .lower()
and check that all letters are from the same row.
Complexity
Space complexity is O(26)
, time complexity is O(m)
, where m
is total number of symbols in our data.
Code
class Solution:
def findWords(self, words):
keys = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
d = {}
for i, line in enumerate(keys):
for s in line: d[s] = i
return [word for word in words if len(set(d[s] for s in word.lower())) == 1]