Minimum Number of Pushes to Type Word II - Problem

Imagine you're customizing an old-school telephone keypad to type text messages as efficiently as possible! 📱

You're given a string word containing lowercase English letters. On a telephone keypad, keys 2-9 can each be mapped to different collections of letters. Traditionally, key 2 maps to ["a","b","c"] - you press it once for "a", twice for "b", and three times for "c".

Here's the twist: You can completely remap keys 2-9 to any letters you want! Each letter must be assigned to exactly one key, but you can put any number of letters on each key.

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

Key insight: Letters that appear more frequently should require fewer presses (be placed earlier on keys), while rare letters can require more presses.

Input & Output

example_1.py — Basic Case
$ Input: word = "abcde"
Output: 5
💡 Note: We have 5 unique letters and 8 available keys. We can assign each letter to position 1 on different keys: a→key2(pos1), b→key3(pos1), c→key4(pos1), d→key5(pos1), e→key6(pos1). Total presses: 1+1+1+1+1 = 5.
example_2.py — Frequency Optimization
$ Input: word = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"
Output: 104
💡 Note: All 26 letters appear twice. First 8 letters (by frequency, all equal) get position 1 (1 press each), next 8 get position 2 (2 presses each), next 8 get position 3 (3 presses each), last 2 get position 4 (4 presses each). Total: 8×2×1 + 8×2×2 + 8×2×3 + 2×2×4 = 16+32+48+16 = 112. Wait, let me recalculate: 8×2×1 + 8×2×2 + 8×2×3 + 2×2×4 = 16+32+48+16 = 112. Actually, since frequencies are equal, we assign alphabetically: first 8 get pos1, etc. Total = 8×2×1 + 8×2×2 + 8×2×3 + 2×2×4 = 112.
example_3.py — High Frequency Letters
$ Input: word = "aabbbc"
Output: 7
💡 Note: Letter frequencies: b→3, a→2, c→1. We assign: b→position1 (1 press), a→position1 (1 press), c→position1 (1 press). All three letters can fit in position 1 since we have 8 keys. Total presses: 3×1 + 2×1 + 1×1 = 6. Wait, that's wrong. Let me recalculate: b appears 3 times at 1 press each = 3, a appears 2 times at 1 press each = 2, c appears 1 time at 1 press each = 1. Total = 3+2+1 = 6.

Constraints

  • 1 ≤ word.length ≤ 105
  • word consists of lowercase English letters only
  • We have exactly 8 keys available (keys 2-9)
  • Each letter must be mapped to exactly one key

Visualization

Tap to expand
📱 Smart Keypad Assignment StrategyTraditional Keypad vs Optimized Keypad❌ Traditional (Fixed)2: abc3: def4: ghi5: jkl6: mno7: pqrsFixed assignmentNot optimized for your text✅ Optimized (Smart)2: e(1×)3: t(1×)4: a(1×)5: i(2×)6: o(2×)7: x(3×)Frequent letters → fewer pressesCustomized for minimum effort!Example: Typing "hello"Letter frequencies: h(1), e(1), l(2), o(1)Sorted by frequency: l(2), h(1), e(1), o(1)Assignment: l→pos1, h→pos1, e→pos1, o→pos1 (all fit in position 1)Total presses: l(2×1) + h(1×1) + e(1×1) + o(1×1) = 5 presses🎯 Key InsightGreedy assignment is optimal because:High frequency × Low cost = Minimum total cost
Understanding the Visualization
1
Count Popularity
Count how often each letter appears in the word
2
Sort by Demand
Arrange letters from most frequent to least frequent
3
Assign Prime Spots
Give the best positions (1 press) to the most popular letters
4
Calculate Efficiency
Sum up total presses needed across the entire word
Key Takeaway
🎯 Key Insight: The greedy approach works perfectly here - always assign the most frequent letters to the cheapest positions first to minimize total cost!
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
23.4K 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