Skip to main content
LeetCode 681, Medium. Topics: Hash Table, String, Backtracking, Enumeration. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

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.

Examples

Constraints

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

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026