Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c".
It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.
Return the minimum number of pushes needed to typewordafter remapping the keys.
An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.
Input: word ="abcde"Output: 5Explanation: The remapped keypad given in the image provides the minimum cost."a"-> one push on key 2"b"-> one push on key 3"c"-> one push on key 4"d"-> one push on key 5"e"-> one push on key 6Total cost is1+1+1+1+1=5.It can be shown that no other mapping can provide a lower cost.
Example 2:
1
2
3
4
5
6
7
8
9
Input: word ="xyzxyzxyzxyz"Output: 12Explanation: The remapped keypad given in the image provides the minimum cost."x"-> one push on key 2"y"-> one push on key 3"z"-> one push on key 4Total cost is1*4+1*4+1*4=12It can be shown that no other mapping can provide a lower cost.Note that the key 9is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
Example 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: word ="aabbccddeeffgghhiiiiii"Output: 24Explanation: The remapped keypad given in the image provides the minimum cost."a"-> one push on key 2"b"-> one push on key 3"c"-> one push on key 4"d"-> one push on key 5"e"-> one push on key 6"f"-> one push on key 7"g"-> one push on key 8"h"-> two pushes on key 9"i"-> one push on key 9Total cost is1*2+1*2+1*2+1*2+1*2+1*2+1*2+2*2+6*1=24.It can be shown that no other mapping can provide a lower cost.
defminimum_pushes(word: str) -> int:
from collections import Counter
# Count the frequency of each letter freq = [0] *26for char in word:
freq[ord(char) - ord("a")] +=1# Sort the frequency array in descending order freq.sort(reverse=True)
ans =0 i =0 presses =1# Array of counts representing the slots for key presses 1 through 4 counts = [8, 8, 8, 2]
# Iterate through each set of counts and calculate the minimum pressesfor count in counts:
while i <26and freq[i] !=0and count >0:
ans += presses * freq[i]
count -=1 i +=1 presses +=1return ans
To solve this problem, we need to find a way to map the letters to the keys such that the total number of pushes required to type the given string word is minimized. Here’s a clear plan to achieve that:
Frequency Count:
First, count the frequency of each letter in the word. This tells us how many times each letter appears.
Sort by Frequency:
Sort the letters by their frequency in descending order. This way, the most frequent letters will be assigned to the keys that require the fewest presses.
Assign Letters to Keys:
Start assigning the letters to keys from 2 to 9, distributing the letters to each key in a way that minimizes the total number of presses.
classSolution {
publicstaticintminPushesToType(String word) {
// Step 1: Count frequency of each letter Map <Character, Integer> frequencyMap =new HashMap<>();
for (char c: word.toCharArray()) {
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}
// Step 2: Use a max heap to sort frequencies in descending order PriorityQueue<Integer> maxHeap =new PriorityQueue<>((a, b) -> b - a);
for (int frequency: frequencyMap.values()) {
maxHeap.offer(frequency);
}
int minPresses = 0;
int presses = 1;
// Step 3: Assign frequencies to keys in the order of their need of presses.while (!maxHeap.isEmpty()) {
for (int i = 0; i < 8; i++) {
// Distribute the letters among the keysif (maxHeap.isEmpty()) {
break;
}
int freq = maxHeap.poll();
minPresses += freq * presses;
}
presses++;
}
return minPresses;
}
}
defmin_pushes_to_type(word: str) -> int:
from collections import Counter
import heapq
# Step 1: Count frequency of each letter frequency = Counter(word)
# Step 2: Sort letters by frequency in descending order freq_list = sorted(frequency.values(), reverse=True)
# Step 3: Calculate minimum presses needed min_presses =0# Highest priority values should be pushed to the front heapq.heapify(freq_list) # Convert list to a min heap presses =1while freq_list:
for _ in range(8): # Distribute the letters among the keysifnot freq_list:
break min_presses += heapq.heappop(freq_list) * presses
presses +=1return min_presses