Next Closest Time - Problem

Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Return the next closest time in the same format "HH:MM".

Input & Output

Example 1 — Basic Case
$ Input: time = "19:34"
Output: "19:41"
💡 Note: The next closest time using digits {1,3,4,9} is 19:41. We increment the minutes from 34 to 41 using available digits.
Example 2 — Wrap to Next Hour
$ Input: time = "23:59"
Output: "22:22"
💡 Note: Available digits are {2,3,5,9}. Since no valid time exists after 23:59 on same day using these digits, we wrap to next day with smallest time: 22:22.
Example 3 — Same Digits
$ Input: time = "00:00"
Output: "00:00"
💡 Note: Only digit 0 is available. The next closest time using only 0s is still 00:00 (wraps around).

Constraints

  • time.length == 5
  • time is in the format "HH:MM"
  • 0 ≤ HH ≤ 23
  • 0 ≤ MM ≤ 59

Visualization

Tap to expand
Next Closest Time ProblemInput Time:19:34Available: {1,3,4,9}Find Next Time Using:• Only digits {1,3,4,9}• Valid HH:MM formatNext Time:19:41Minimal incrementChallenge: Use only available digits to form valid time19:35, 19:36, 19:37, 19:38, 19:40 are invalid (digits 0,2,5,6,7,8 not available)
Understanding the Visualization
1
Input
Time string in HH:MM format with available digits
2
Process
Find next time using only these digits
3
Output
Return next closest valid time
Key Takeaway
🎯 Key Insight: Reuse only the available digits from input time to minimize the next valid time increment
Asked in
Google 25 Facebook 18 Amazon 12
32.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen