| 1 | from collections import Counter |
| 2 | |
| 3 | def solution(word): |
| 4 | charsCount = Counter(word) |
| 5 | mostCommonChars = charsCount.most_common() |
| 6 | |
| 7 | keyPad = [[] for i in range(9)] |
| 8 | totalClickCounts = 0 |
| 9 | currKeypadIndex = 1 |
| 10 | for char, occ in mostCommonChars: |
| 11 | keyPad[currKeypadIndex - 1].append(char) |
| 12 | buttonPosition = keyPad[currKeypadIndex - 1].index(char) + 1 # since lists are 0-indexed and to avoid zero multiplication |
| 13 | |
| 14 | totalClickCounts += occ * buttonPosition |
| 15 | currKeypadIndex = (currKeypadIndex + 1) % 9 |
| 16 | |
| 17 | print(keyPad) |
| 18 | return totalClickCounts |
| 19 | |
| 20 | word1 = "abacadefghibj" |
| 21 | word2 = "abcghdiefjoba" |