{x}
blog image

Minimum Number of Keypresses

Solution Explanation: Minimum Number of Keypresses

This problem involves finding the minimum number of keypresses needed to type a given string, given constraints on the mapping of characters to buttons on a keypad. The constraints are:

  • Each of the 26 lowercase English letters must be mapped to a button.
  • Each character is mapped to exactly one button.
  • Each button can map to at most 3 characters.

The optimal strategy is to assign the most frequent characters to the buttons, and then assign the next most frequent characters to the same buttons. This is a greedy approach.

Algorithm

  1. Count Character Frequencies: First, count the occurrences of each character in the input string s. We can use a hash table (or a simple array since we only have 26 lowercase letters) for efficient counting.

  2. Sort Frequencies: Sort the character frequencies in descending order. The characters with the highest frequencies will require the fewest key presses.

  3. Assign to Buttons: Iterate through the sorted frequencies and assign characters to buttons. Since each button can map to at most 3 characters, we'll cycle through the 9 buttons repeatedly. For example:

    • The 9 most frequent characters go to buttons 1-9 (one press each).
    • The next 9 most frequent characters go to buttons 1-9 again (two presses each).
    • And so on.
  4. Calculate Total Keypresses: For each character, the number of keypresses is determined by its position within its button's assignment:

    • First character on a button: 1 keypress.
    • Second character: 2 keypresses.
    • Third character: 3 keypresses.
  5. Return Total: Sum up the keypresses for all characters to get the minimum total.

Time and Space Complexity

  • Time Complexity: O(N + K log K), where N is the length of the string and K is the number of unique characters (26 in this case). The dominant factor is sorting the frequencies (O(K log K)). Counting character frequencies is O(N).

  • Space Complexity: O(K) to store character frequencies. Since K is a constant (26), the space complexity is considered O(1).

Code Implementation (Python)

from collections import Counter
 
def minimumKeypresses(s: str) -> int:
    """
    Calculates the minimum number of keypresses needed to type the string s.
    """
    # Count character frequencies
    freq = Counter(s)
    frequencies = sorted(freq.values(), reverse=True)
 
    total_keypresses = 0
    keypad_index = 0  # Index of the current button (0-8)
    press_count = 1   # Number of presses for the current character on a button
 
    for i, count in enumerate(frequencies):
        total_keypresses += count * press_count
 
        press_count += 1
        if press_count > 3:
            press_count = 1 #Reset for the next button
            keypad_index = (keypad_index + 1) % 9  # Cycle through buttons
 
 
    return total_keypresses
 

The code in other languages (Java, C++, Go, TypeScript) follows a very similar structure, using appropriate data structures and syntax for each language. The core algorithmic steps remain the same.