Skip to content

Commit 10af7de

Browse files
authored
Create single-row-keyboard.py
1 parent e3f70ac commit 10af7de

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Python/single-row-keyboard.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def calculateTime(self, keyboard, word):
6+
"""
7+
:type keyboard: str
8+
:type word: str
9+
:rtype: int
10+
"""
11+
lookup = {c:i for i, c in enumerate(keyboard)}
12+
result, prev = 0, 0
13+
for c in word:
14+
result += abs(lookup[c]-prev)
15+
prev = lookup[c]
16+
return result

0 commit comments

Comments
 (0)