Minimum Number of Pushes to Type Word I - Problem

Imagine you have an old-school telephone keypad and need to type a word using T9-style input! ๐Ÿ“ฑ

You're given a string word containing distinct lowercase English letters. On a telephone keypad, each key (2-9) can be mapped to multiple letters. For example:

  • Key 2 might have ["a","b","c"] - press once for "a", twice for "b", three times for "c"
  • Key 3 might have ["d","e","f"] - press once for "d", twice for "e", etc.

The Challenge: You can remap the 8 keys (2-9) to any arrangement of letters you want! Each letter must be assigned to exactly one key, but keys can have different numbers of letters.

Goal: Find the minimum total number of key presses needed to type the entire word after optimally remapping the keys.

Key Insight: Since all letters in the word are distinct, this becomes an optimization problem about how to distribute letters across keys to minimize total presses!

Input & Output

example_1.py โ€” Basic case
$ Input: word = "abc"
โ€บ Output: 3
๐Ÿ’ก Note: We have 3 distinct letters. Optimally assign each to first position of 3 different keys: aโ†’key2(1 press), bโ†’key3(1 press), cโ†’key4(1 press). Total: 1+1+1 = 3 presses.
example_2.py โ€” More letters
$ Input: word = "abcdefghijklmnop"
โ€บ Output: 24
๐Ÿ’ก Note: 16 distinct letters. First 8 letters get 1 press each (8 total), remaining 8 letters get 2 presses each (16 total). Total: 8+16 = 24 presses.
example_3.py โ€” Single letter repeated
$ Input: word = "aaaaaa"
โ€บ Output: 1
๐Ÿ’ก Note: Only 1 distinct letter 'a'. Assign it to first position of any key, requiring only 1 press total.

Constraints

  • 1 โ‰ค word.length โ‰ค 105
  • word consists of distinct lowercase English letters
  • The number of distinct letters is at most 26

Visualization

Tap to expand
๐Ÿ“ฑ Phone Keypad StrategyOld School Phone2ABC3DEF4GHI5JKL6MNO7PQR8STU9VWXOptimization Strategy๐ŸŽฏ Goal: Minimize Total Pressesโ€ข We have 8 keys available (2, 3, 4, 5, 6, 7, 8, 9)โ€ข Each key can hold multiple lettersโ€ข Position on key determines presses neededโ€ข All letters in word are distinct (appear once)๐Ÿ’ก Key InsightSince letters are distinct, we want to minimizethe number of letters in higher positions:Position 1: 8 letters ร— 1 press = 8Position 2: 8 letters ร— 2 presses = 16Example: 15 distinct lettersPosition 1: letters 1-8 โ†’ 8 ร— 1 = 8 pressesPosition 2: letters 9-15 โ†’ 7 ร— 2 = 14 pressesTotal: 8 + 14 = 22 presses
Understanding the Visualization
1
Count Your Letters
First, figure out how many unique letters you need to type
2
Fill the Prime Spots
Put the first 8 letters in the best positions (1 press each) across all 8 keys
3
Use Second-Best Spots
Put the next 8 letters in position 2 (2 presses each) across the keys
4
Continue the Pattern
Keep filling positions 3, 4, etc. until all letters are assigned
5
Calculate Total Cost
Add up all the presses: 8ร—1 + 8ร—2 + 8ร—3 + ...
Key Takeaway
๐ŸŽฏ Key Insight: Since all letters are distinct, we use a greedy approach to place letters optimally across key positions, minimizing total presses by filling position 1 first (8 letters ร— 1 press), then position 2 (8 letters ร— 2 presses), and so on.
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
24.6K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen