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
Constraints
- 1 ≤ word.length ≤ 105
-
wordconsists of lowercase English letters only - We have exactly 8 keys available (keys 2-9)
- Each letter must be mapped to exactly one key