Slowest Key

Updated on 11 July, 2025
Slowest Key header image

Problem Statement

In an evaluation of a new keypad design, a tester goes through a sequence of key presses. Each key, identified by characters in the string keysPressed, is pressed exactly when the previous key was released, starting from time 0. The times when each key is released are recorded in the list releaseTimes. To determine a key's hold duration, the difference between its release time and the previous key's release time is calculated. The initial key's hold time is simply its release time since it starts from 0.

The challenge is to identify the key that was held down the longest during the test. If multiple keys share the longest duration, the key that is lexicographically larger should be returned. This evaluation must consider possible multiple presses of the same key, each potentially having different durations.

Examples

Example 1

Input:

releaseTimes = [9,29,49,50], keysPressed = "cbcd"

Output:

"c"

Explanation:

The keypresses were as follows:
Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).
Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).
Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).
The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
'c' is lexicographically larger than 'b', so the answer is 'c'.

Example 2

Input:

releaseTimes = [12,23,36,46,62], keysPressed = "spuda"

Output:

"a"

Explanation:

The keypresses were as follows:
Keypress for 's' had a duration of 12.
Keypress for 'p' had a duration of 23 - 12 = 11.
Keypress for 'u' had a duration of 36 - 23 = 13.
Keypress for 'd' had a duration of 46 - 36 = 10.
Keypress for 'a' had a duration of 62 - 46 = 16.
The longest of these was the keypress for 'a' with duration 16.

Constraints

  • releaseTimes.length == n
  • keysPressed.length == n
  • 2 <= n <= 1000
  • 1 <= releaseTimes[i] <= 109
  • releaseTimes[i] < releaseTimes[i+1]
  • keysPressed contains only lowercase English letters.

Approach and Intuition

Understanding the longest keypress requires calculating the duration each key was held. We can break down the solution into the following steps:

  1. Initialize variables to track the longest duration and the key associated with it.
  2. Iterate through the keysPressed and releaseTimes arrays:
    • For the first key (index 0), the duration is simply releaseTimes[0].
    • For each subsequent key, the duration is computed as the difference between its release time and the previous key’s release time.
  3. Update the longest duration and corresponding key if:
    • The current key's duration is greater than the currently recorded longest duration.
    • The current key's duration equals the longest duration, but the key is lexicographically larger.
  4. The key associated with the longest duration at the end of the iterations is the answer.

This approach ensures that each keypress's duration is evaluated, and through a direct comparison, the correct key is selected, respecting both the duration and lexicographical precedence rules.

Solutions

  • C++
cpp
class Solution {
public:
    char findSlowestKey(vector<int>& releaseTimes, string keysPressed) {
        int numberOfKeys = releaseTimes.size();
        int maxDuration = releaseTimes[0];
        char resultKey = keysPressed[0];
        for (int i = 1; i < numberOfKeys; i++) {
            int duration = releaseTimes[i] - releaseTimes[i - 1];
            if (duration > maxDuration ||
                (duration == maxDuration && keysPressed[i] > resultKey)) {
                maxDuration = duration;
                resultKey = keysPressed[i];
            }
        }
        return resultKey;
    }
};

This solution, implemented in C++, is designed to determine the slowest key from a series of key presses. It involves calculating the longest duration a key remains pressed based on timing data provided for each key release.

Follow these steps in the provided code to understand how to find the slowest key:

  1. Initialize numberOfKeys with the size of releaseTimes to get the total number of key presses.
  2. Start by assigning maxDuration to the duration of the first key press (releaseTimes[0]), and resultKey to the first key in keysPressed.
  3. Iterate through the releaseTimes starting from the second key press (index 1).
  4. Calculate the duration for the current key by subtracting the previous release time from the current one.
  5. Compare the duration of the current key with maxDuration.
    • If the duration is greater, update maxDuration and set resultKey to the current key.
    • If the duration is equal to the maxDuration, further compare the keys. Update resultKey if the current key is lexicographically greater.
  6. Once the loop completes, resultKey will store the character of the slowest key.

This approach ensures that you efficiently find the key with the longest press time, considering all edge cases, such as when two keys have the same duration.

  • Java
java
class Solution {
    
    public char findSlowestKey(int[] pressTimes, String keySequence) {
        int count = pressTimes.length;
        int maxDuration = pressTimes[0];
        char resultKey = keySequence.charAt(0);
        for (int index = 1; index < count; index++) {
            int duration = pressTimes[index] - pressTimes[index - 1];
            // Determine if the current key has the longest press
            if (duration > maxDuration ||
                (duration == maxDuration && keySequence.charAt(index) > resultKey)) {
                maxDuration = duration;
                resultKey = keySequence.charAt(index);
            }
        }
        return resultKey;
    }
}

The provided Java solution efficiently determines the slowest key from a sequence of key presses. The method findSlowestKey accepts two parameters: pressTimes, an array of integers where each integer represents the time at which a key is pressed, and keySequence, a string where each character represents a specific key pressed at the corresponding time in the pressTimes array.

  • Initialize count to the length of the pressTimes array.
  • Set maxDuration to the duration of the first key pressed, as indicated by the first element in the pressTimes.
  • Set resultKey to the first character of the keySequence.
  • Loop through each subsequent key press starting from the second. Calculate the duration of each key press by subtracting the press time of the previous key from the current key's press time.
  • Compare the calculated duration with the recorded maxDuration. If the current duration is greater or if it is the same but the key itself has a higher lexicographical order than the current slowest key, update maxDuration and resultKey.
  • Finally, return the key (resultKey) with the longest press time or the lexicographically greater key in case of tie durations, which is considered the slowest.

This solution effectively utilizes a for-loop and conditional statements to compare each key's press duration and ensures the correct slowest key is identified with a time complexity of O(n), where n is the number of keys pressed.

Comments

No comments yet.