Minimum Number of Pushes to Type Word II

A telephone keypad has keys `2` to `9`. You may remap the lowercase letters onto those 8 keys any way you like: each letter goes to exactly one key, and a key may hold any number of letters. Typing the `k`th letter on a key takes `k` pushes of that key. Given a string `word` of lowercase letters, return the minimum total number of pushes needed to type `word` after remapping the keys optimally. Keys `1`, `*`, `#` and `0` hold no letters.

30:00

Minimum Number of Pushes to Type Word II
medium
Free written solution
Topics
Companies

A telephone keypad has keys 2 to 9. You may remap the lowercase letters onto those 8 keys any way you like: each letter goes to exactly one key, and a key may hold any number of letters. Typing the kth letter on a key takes k pushes of that key. Given a string word of lowercase letters, return the minimum total number of pushes needed to type word after remapping the keys optimally. Keys 1, *, # and 0 hold no letters.

Example 1:
Input: {"word":"abcde"}
Output: 5
Constraints:
  • 1≤word.length≤1051 \leq \text{word.length} \leq 10^5

  • word\text{word} consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word":"abcde"}
a
b
c
d
e

letter counts

nothing counted yet

2

3

4

5

6

7

8

9

■ slot 1 = 1 push

■ slot 2 = 2 pushes

■ slot 3 = 3 pushes

■ slot 4 = 4 pushes

We choose the mapping. Each key has slot 1 (one push), slot 2 (two pushes) and so on, and there are 8 keys. Count how often each letter is typed first.

total pushes = 0
Variables
VariableValue
pushes0
DepthFunction Call
Stack empty
0/13