-
Notifications
You must be signed in to change notification settings - Fork 2
/
1165.py
35 lines (31 loc) · 953 Bytes
/
1165.py
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
# [ LeetCode ] 1165. Single-Row Keyboard
def solution(keyboard: str, word: str) -> int:
answer: int = 0
current_index: int = 0
keyboard_position: dict[str, int] = {
letter: index for index, letter in enumerate(keyboard)
}
for letter in word:
position: int = keyboard_position[letter]
answer += abs(position - current_index)
current_index = position
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, str] | int]] = [
{
"input": {
"keyboard": "abcdefghijklmnopqrstuvwxyz",
"word": "cba"
},
"output": 4
},
{
"input": {
"keyboard": "pqrstuvwxyzabcdefghijklmno",
"word": "leetcode"
},
"output": 73
}
]
for case in cases:
assert case["output"] == solution(**case["input"])