-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathsolution.py
More file actions
24 lines (18 loc) · 761 Bytes
/
solution.py
File metadata and controls
24 lines (18 loc) · 761 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
from collections import Counter
def solution(word):
charsCount = Counter(word)
mostCommonChars = charsCount.most_common()
keyPad = [[] for i in range(9)]
totalClickCounts = 0
currKeypadIndex = 1
for char, occ in mostCommonChars:
keyPad[currKeypadIndex - 1].append(char)
buttonPosition = keyPad[currKeypadIndex - 1].index(char) + 1 # since lists are 0-indexed and to avoid zero multiplication
totalClickCounts += occ * buttonPosition
currKeypadIndex = (currKeypadIndex + 1) % 9
print(keyPad)
return totalClickCounts
word1 = "abacadefghibj"
word2 = "abcghdiefjoba"
print(f"Min num of clicks (1) : {solution(word1)}" )
print(f"Min num of clicks (2) : {solution(word2)}" )