Ctrl K

Next Time From the Same Digits

Medium
A digital clock shows the time as "HH:MM" in 24-hour format. You may build a new time from the digits currently on the display, using each digit as many times as you like. Given the current time clock, return the nearest later time that can be written with those digits only, in the same format. If no such time is left today, the answer is a time tomorrow (possibly the same time, 24 hours later). For example, "12:09" lets you build "10:22" but not "13:00": there is no 3 on the display.
Example 1
clock="14:48" → '18:11'

The digits are 1, 4 and 8. None of the minutes 49–59 can be spelled with them, so the next option is 18:11, 3 hours and 23 minutes later.

Example 2
clock="20:09" → '20:20'

With the digits 0, 2 and 9, the next time is 20:20, only 11 minutes later.

Example 3
clock="22:22" → '22:22'

Only the digit 2 is available, and the only time it spells is 22:22 itself, a full day later.

Constraints
  • len(clock) == 5
  • clock is a valid "HH:MM" time: 00 ≤ HH ≤ 23, 00 ≤ MM ≤ 59
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKeypad Words MediumPalindromes From a Set of Letters MediumDoes the String Fit the Letter Template MediumRestore the Lost Point MediumCount the Tile Sequences Medium
loading Python…
Input
clock="14:48"
Expected
'18:11'

Run checks the first 3 tests in your browser. Submit checks all 136 tests on the server, hidden ones included.