1070. Next Clock Time

Medium3.5 points estimatedStringEnumerationHash Table3 hints

After a power cut, a station clock is restarted. Its display shows the 24-hour time time in the form HH:MM, but from now on only the lamps for digits that appear in time still work, so every later reading may use only those digits (each as often as needed). Moving forward in time from time, find the first reading that the repaired clock can show, wrapping past midnight into the next day if necessary.

Return that reading as HH:MM. The reading must come strictly later than time; if no other reading is possible, a full day later you get time itself. Build candidates from the allowed digits or scan the 1440 minutes: O(1) time and space since the clock is fixed size.

Example 1

Input:
time = "08:20"
Output:
"08:22"
Explanation:

Only digits 0, 2 and 8 work, and the very next minute 08:21 uses a 1, but 08:22 uses only allowed digits.

Example 2

Input:
time = "17:57"
Output:
"11:11"
Explanation:

With digits 1, 5 and 7 there is no later reading today, so the clock wraps to the next day and first shows 11:11.

Example 3

Input:
time = "06:44"
Output:
"06:46"
Explanation:

Digits 0, 4 and 6 allow 06:46 as the first later reading.

Constraints

time is a valid time "HH:MM" with 00 ≤ HH ≤ 23 and 00 ≤ MM ≤ 59

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…