Minimum Number of Pushes to Type Word II (LeetCode 3016): Solution and Explanation
You may remap the 26 letters onto the 8 phone keys 2 to 9 however you like, and the kth letter on a key costs k pushes. Given word, return the fewest total pushes needed to type it.
Intuition
Where a letter sits in the word does not matter, only how many times it is typed. A letter typed f times in slot k costs f * k pushes in total.
Eight keys means eight slots that cost one push, eight that cost two, eight that cost three and two that cost four. The cost of a mapping is the sum of frequency times slot cost.
To minimize a sum of products you pair the largest frequencies with the smallest costs. If a rarer letter sat in a cheaper slot than a more frequent one, swapping them would lower the total.
So sort the frequencies from largest to smallest: the letter at sorted index i gets slot i // 8 + 1.
Approach: Greedy by frequency
- Count each letter into
freq[26]. - Sort
freqin descending order. - For i from 0 to 25, stop at the first zero count; otherwise add
freq[i] * (i // 8 + 1)to the total. - Return the total.
Step-by-step example
word = "aabbccddeeffgghhiiiiii"
- Counts: i = 6, and a through h = 2 each.
- Sorted: 6, 2, 2, 2, 2, 2, 2, 2, 2.
- Indices 0 to 7 take one-push slots: 6 + 7 * 2 = 20.
- Index 8 is the first two-push slot: 2 * 2 = 4.
- Index 9 is 0, so stop. The answer is 24.
Code: Python, Java, C++
1class Solution:
2 def minimumPushes(self, word: str) -> int:
3 freq = [0] * 26
4 for ch in word:
5 freq[ord(ch) - ord('a')] += 1
6 freq.sort(reverse=True)
7 pushes = 0
8 for i in range(26):
9 if freq[i] == 0:
10 break
11 pushes += freq[i] * (i // 8 + 1)
12 return pushes1class Solution {
2 public int minimumPushes(String word) {
3 int[] freq = new int[26];
4 for (char ch : word.toCharArray()) {
5 freq[ch - 'a']++;
6 }
7 Arrays.sort(freq);
8 int pushes = 0;
9 for (int i = 0; i < 26; i++) {
10 int f = freq[25 - i];
11 if (f == 0) {
12 break;
13 }
14 pushes += f * (i / 8 + 1);
15 }
16 return pushes;
17 }
18}1class Solution {
2public:
3 int minimumPushes(string word) {
4 vector<int> freq(26, 0);
5 for (char ch : word) {
6 freq[ch - 'a']++;
7 }
8 sort(freq.begin(), freq.end(), greater<int>());
9 int pushes = 0;
10 for (int i = 0; i < 26; i++) {
11 if (freq[i] == 0) {
12 break;
13 }
14 pushes += freq[i] * (i / 8 + 1);
15 }
16 return pushes;
17 }
18};Time and space complexity
O(n)
Counting is one pass over the word; sorting 26 numbers is constant work.
O(1)
Only a fixed array of 26 counts.
Other approaches
Max-heap of frequencies (time O(n), space O(1))
Push the non-zero counts into a max-heap and pop them in order, assigning the next slot each time. Same result, more code.
Common mistakes
- Assigning letters in alphabetical or first-seen order instead of by frequency.
- Using
i // 9or forgetting there are only 8 usable keys (2 to 9). - Sorting ascending and then indexing from the front, which puts the rarest letters in the cheapest slots.
- Counting distinct letters only, as in version I of the problem, where every letter appears once.
Frequently asked questions
Why is greedy by frequency optimal?
It is the rearrangement inequality: a sum of products is smallest when one sequence is sorted ascending and the other descending. Slot costs only rise, so frequencies must fall.
How does this differ from version I?
In version I every letter is distinct, so all frequencies are 1 and only the count of letters matters. Version II repeats letters, so frequent ones must take the cheap slots.
What is the time complexity?
O(n) time for counting and O(1) extra space, because the sort is over 26 values.