Skip to main content

Next Closest Time - Solution & Explanation

MediumPremiumFree on FleetCodeHash TableStringBacktrackingEnumeration4 min readAsked at: Google
Practice this problem

Problem Statement

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.

 

Example 1:

Input: time = "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later.
It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

Input: time = "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22.
It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

 

Constraints:

  • time.length == 5
  • time is a valid time in the form "HH:MM".
  • 0 <= HH < 24
  • 0 <= MM < 60

Approach Overview

Problem Overview: Given a time in HH:MM format, return the next closest valid time that can be formed using only the digits already present in the original time. Digits can be reused any number of times, but the result must represent a valid 24‑hour clock time.

Approach 1: Minute Simulation with Hash Set (O(1) time, O(1) space)

Extract the four digits from the input time and store them in a hash set for constant‑time membership checks. Convert the current time into total minutes since midnight. Then simulate minute by minute: increment the minute count, wrap around using modulo 24 * 60, and rebuild the candidate time. Split the candidate back into digits and verify every digit exists in the set. The first valid match is the answer. The search space is bounded to 1440 minutes, so the complexity is constant in practice. This approach relies on simple string manipulation and avoids complicated generation logic.

Approach 2: Digit Enumeration / Backtracking (O(4^4) time, O(1) space)

Instead of scanning minute by minute, enumerate every possible 4‑digit combination using only the allowed digits. Use backtracking or iterative enumeration to generate all combinations for positions HHMM. For each candidate, validate that hours are 0–23 and minutes are 0–59. Convert the valid candidate into minutes and compute the time difference from the original time (handling wrap‑around at midnight). Track the smallest positive difference. This approach explores at most 4^4 = 256 combinations, making it effectively constant time. Enumeration provides tighter control over candidate generation and is often easier to reason about in interview explanations.

Recommended for interviews: The minute‑simulation approach is the most straightforward and is typically what interviewers expect first. It demonstrates clear reasoning: convert time to minutes, iterate, and validate digits using a hash set. The enumeration/backtracking approach shows stronger algorithmic thinking because you explicitly generate all valid candidates and compare distances. Starting with brute simulation proves correctness quickly; presenting enumeration as an optimization shows deeper understanding of bounded search spaces.

Solution

Code

Python

Java

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Minute Simulation with Hash SetO(1440) ≈ O(1)O(1)Most intuitive approach. Easy to implement and explain during interviews.
Digit Enumeration / BacktrackingO(4^4) ≈ O(1)O(1)When you want to explicitly generate all valid candidate times and compute the closest difference.

Video Solution

Next Closest TimeKevin Naughton Jr.32,675 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Next Closest Time easy or hard?
Next Closest Time is rated Medium difficulty on LeetCode. The logic is simple once you recognize the bounded search space, but careful handling of time conversion, digit validation, and midnight wrap‑around makes it slightly tricky.
Next Closest Time Python/Java solution
Python and Java solutions typically convert the time into minutes, store allowed digits in a set, and iterate forward until a valid combination appears. Both implementations rely on simple arithmetic and string formatting rather than complex data structures.
How to solve Next Closest Time in O(n)?
Convert the given time to total minutes and store its digits in a hash set. Increment the minute value and rebuild the candidate time using modulo 1440 for wrap‑around. Validate whether all digits of the candidate appear in the set. The first valid time encountered is the next closest time.
What is the best approach for Next Closest Time?
Minute simulation using a hash set is the most practical approach. Convert the time to minutes, increment minute by minute, and check whether the digits of the new time exist in the allowed digit set. The search space is limited to 1440 minutes, so the runtime is effectively O(1).
Is Next Closest Time asked at Google/Amazon/Meta?
Next Closest Time has appeared in interviews and preparation lists associated with companies like Google and Amazon. It tests reasoning about constrained search spaces, time manipulation, and careful validation of generated candidates.
What data structure is used in Next Closest Time?
A hash set is commonly used to store the allowed digits from the original time. This enables constant‑time membership checks when validating candidate times during simulation or enumeration.
What is the time complexity of Next Closest Time?
Both common approaches run in constant time because the search space is bounded. Minute simulation checks at most 1440 times, giving O(1440) ≈ O(1). Enumeration generates at most 4^4 = 256 digit combinations, which is also constant.

Ready to solve this problem?

Practice Next Closest Time with our built-in code editor and test cases.

Practice on FleetCode