Minimum Number of Pushes to Type Word I — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

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.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Minimum Number of Pushes to Type Word I
Detailed explanation and reference materials
Problem Overview

3014. Minimum Number of Pushes to Type Word I

Problem Overview

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:

  • Every letter must be assigned to exactly one key.
  • A key may contain any number of letters.
  • The order of letters on a key determines the number of key presses:
    • 1st letter → 1 push
    • 2nd letter → 2 pushes
    • 3rd letter → 3 pushes
    • and so on.

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.


Key Insight

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:

  • The next 8 letters occupy the second position2 pushes each.
  • The next 8 letters occupy the third position3 pushes each.
  • Any remaining letters occupy the fourth position4 pushes each.

Because every character appears exactly once and all letters are distinct, the optimal strategy is simply to assign letters to the cheapest available positions.


Example 1

Input

word = "abcde"

Output

5

Explanation

There are only 5 letters, so each can be placed as the first letter on a different key.

LetterPushes
a1
b1
c1
d1
e1

Total pushes:

1 + 1 + 1 + 1 + 1 = 5

Example 2

Input

word = "xycdefghij"

Output

12

Explanation

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:

LetterPushes
x1
c1
e1
f1
g1
h1
i1
j1
y2
d2

Total pushes:

1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 = 12

Constraints

  • 1 <= word.length <= 26
  • word contains only lowercase English letters.
  • Every character in word is distinct.

SEO Keywords

  • Minimum Number of Pushes to Type Word I
  • LeetCode 3014
  • Telephone Keypad Optimization
  • Greedy Algorithm
  • Keypad Remapping Problem
  • Minimum Key Presses
  • Distinct Characters
  • LeetCode Easy Greedy
  • DSA Greedy Problems
  • Algorithm Visualization

Why This Problem Matters

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:

  • Greedy algorithms
  • Resource allocation
  • Cost minimization
  • Optimal assignment
  • Scheduling and placement
Instruction-by-Instruction Breakdown :
Main Function:

Enter Sample Input Here   ---> Input Sentence

Enter Sample Output Here   ---> Output Sentence

public static void main(String[] args) {   ---> Simple Statement

String word1 = "abcde";   ---> Variable Value Assigned

System.out.println(minimumPushes(word1));   ---> Other Function Call

String word2 = "xycdefghij";   ---> Variable Value Assigned

System.out.println(minimumPushes(word2));   ---> Other Function Call

}//function end   ---> Return Statement

Static Helper Function:

public static int minimumPushes(String word) {   ---> Simple Statement

int n = word.length();   ---> Variable Value Assigned

int pushes = 0;   ---> Variable Value Assigned

for (int i = 0; i < n; i++) {   ---> For loop

pushes += (i / 8) + 1;   ---> Variable Value Assigned

}//Loop End   ---> Loop End

return pushes;   ---> Return Statement

}//function end   ---> Return Statement

Static Utility Functions , Classes and Global variables:

Utility Function is not required.

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
strings
greedy
leetcode-problem-of-the-day
maths
java
Reference Link
https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/
Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

Scroll down to play

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.