forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_keyboard_row.py
More file actions
41 lines (31 loc) · 1017 Bytes
/
find_keyboard_row.py
File metadata and controls
41 lines (31 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Keyboard Row Filter
Given a list of words, return the words that can be typed using letters from
only one row of an American QWERTY keyboard.
Reference: https://leetcode.com/problems/keyboard-row/description/
Complexity:
Time: O(n * m) where n is the number of words and m is average word length
Space: O(n)
"""
from __future__ import annotations
_KEYBOARD_ROWS: list[set[str]] = [
set("qwertyuiop"),
set("asdfghjkl"),
set("zxcvbnm"),
]
def find_keyboard_row(words: list[str]) -> list[str]:
"""Return words that can be typed using one keyboard row.
Args:
words: A list of words to check.
Returns:
A list of words each typable on a single keyboard row.
Examples:
>>> find_keyboard_row(["Hello", "Alaska", "Dad", "Peace"])
['Alaska', 'Dad']
"""
result: list[str] = []
for word in words:
for row in _KEYBOARD_ROWS:
if set(word.lower()).issubset(row):
result.append(word)
return result