Back to blog
Sep 24, 2025
5 min read

Slowest Key

Determine the key with the longest duration from release times and key pressed sequence, returning the lexicographically largest key on ties.

Difficulty: Easy | Acceptance: 59.60% | Paid: No Topics: Array, String

A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.

You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed, and a list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed. The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released.

The duration of the ith keypress is releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress has a duration of releaseTimes[0].

Note that the 0th key was pressed at time 0, so its duration is simply releaseTimes[0].

Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key.

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 20, performed by keys 'b' and 'c'. The lexicographically largest key 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 16, performed by key 'a'.

Constraints

2 <= releaseTimes.length <= 1000
1 <= releaseTimes[i] <= 10⁹
releaseTimes and keysPressed have the same length.
releaseTimes is strictly increasing.
keysPressed consists of lowercase English letters.

Single Pass Iteration

Intuition We can iterate through the arrays once, calculating the duration of each keypress on the fly by comparing the current release time with the previous one. We keep track of the maximum duration found so far and the corresponding key, updating the result whenever we find a longer duration or a lexicographically larger key in case of a tie.

Steps

  • Initialize max_duration with releaseTimes[0] and result with keysPressed[0].
  • Iterate from index 1 to the end of the arrays.
  • Calculate current_duration as releaseTimes[i] - releaseTimes[i-1].
  • If current_duration is greater than max_duration, update max_duration and set result to the current key.
  • If current_duration equals max_duration, check if the current key is lexicographically larger than result. If so, update result.
  • Return result after the loop finishes.
python
class Solution:
    def slowestKey(self, releaseTimes: list[int], keysPressed: str) -&gt; str:
        max_dur = releaseTimes[0]
        res = keysPressed[0]
        
        for i in range(1, len(releaseTimes)):
            dur = releaseTimes[i] - releaseTimes[i-1]
            if dur &gt; max_dur or (dur == max_dur and keysPressed[i] &gt; res):
                max_dur = dur
                res = keysPressed[i]
                
        return res

Complexity

  • Time: O(n), where n is the length of the array. We traverse the array exactly once.
  • Space: O(1), we only use a few variables for storage.
  • Notes: This is the most optimal approach as it minimizes both time and space complexity.

Pre-calculation & Comparison

Intuition We can separate the logic into two distinct phases: first, calculate the duration for every single keypress and store them in a new array; second, iterate through this new array to find the maximum duration and the corresponding key. This approach is more verbose but clearly separates the calculation from the comparison logic.

Steps

  • Create a new array durations of the same length as releaseTimes.
  • Set durations[0] to releaseTimes[0].
  • Iterate from index 1 to n-1, calculating durations[i] as releaseTimes[i] - releaseTimes[i-1].
  • Initialize max_duration to 0 and result to an empty character.
  • Iterate through the durations array.
  • If durations[i] is greater than max_duration, update max_duration and result.
  • If durations[i] equals max_duration and keysPressed[i] is lexicographically larger than result, update result.
  • Return result.
python
class Solution:
    def slowestKey(self, releaseTimes: list[int], keysPressed: str) -&gt; str:
        n = len(releaseTimes)
        durations = [0] * n
        durations[0] = releaseTimes[0]
        
        for i in range(1, n):
            durations[i] = releaseTimes[i] - releaseTimes[i-1]
            
        max_dur = -1
        res = ''
        
        for i in range(n):
            if durations[i] &gt; max_dur or (durations[i] == max_dur and keysPressed[i] &gt; res):
                max_dur = durations[i]
                res = keysPressed[i]
                
        return res

Complexity

  • Time: O(n), where n is the length of the array. We iterate through the array twice, which simplifies to O(n).
  • Space: O(n), to store the auxiliary durations array.
  • Notes: This approach uses extra space compared to the single pass method, which is unnecessary for this problem.