How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
You are given a string word containing distinct lowercase English letters.
A telephone keypad contains 8 usable keys (2 to 9). Each key can be assigned any collection of lowercase English letters, but:
Your task is to remap the keypad so that typing the given word requires the minimum total number of key presses.
Return the minimum number of pushes needed to type the entire word.
Since there are 8 available keys, the first 8 letters can each occupy the first position of a key, costing 1 push each.
After all first positions are filled:
Because every character appears exactly once and all letters are distinct, the optimal strategy is simply to assign letters to the cheapest available positions.
Input
word = "abcde"
Output
5
There are only 5 letters, so each can be placed as the first letter on a different key.
| Letter | Pushes |
|---|---|
| a | 1 |
| b | 1 |
| c | 1 |
| d | 1 |
| e | 1 |
Total pushes:
1 + 1 + 1 + 1 + 1 = 5
Input
word = "xycdefghij"
Output
12
There are 10 distinct letters.
The first 8 letters occupy first positions on different keys.
The remaining 2 letters must be placed in second positions.
One optimal assignment is:
| Letter | Pushes |
|---|---|
| x | 1 |
| c | 1 |
| e | 1 |
| f | 1 |
| g | 1 |
| h | 1 |
| i | 1 |
| j | 1 |
| y | 2 |
| d | 2 |
Total pushes:
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 = 12
1 <= word.length <= 26word contains only lowercase English letters.word is distinct.This problem demonstrates an important greedy optimization strategy.
Instead of trying every possible keypad mapping, we realize that only the push cost of each position matters. By always assigning letters to the cheapest available positions first, we achieve the minimum total cost.
This idea appears frequently in interview problems involving:
🎯 Select Challenge to activate
Scroll down to play
▼Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.