A commuter-rail terminal's departure board shows the next boarding time as a 24-hour clock in the form "HH:MM". After a burst of steam from a passing engine, condensation has fogged over some of the digit segments, so those positions render as '?' instead of a digit (the ':' is always clearly visible). The board was displaying some valid time before it fogged up. Reconstruct the latest valid time — the one occurring latest in the day — that is consistent with every digit still visible.
A single line containing a string of exactly 5 characters in the form "HH:MM", where each of the four letters H/M is either a digit '0'-'9' or the character '?', and the third character is always ':'.
A single line containing the reconstructed time, in the same "HH:MM" format, that is the latest valid 24-hour time consistent with the visible digits.
The input is guaranteed to be a string for which at least one assignment of digits to the '?' positions yields a valid 24-hour time, i.e. HH in the range 00-23 and MM in the range 00-59.
Example 1
Input
1?:?9
Expected
19:59
Explanation
The hour's tens digit is fixed at '1', so the hour is somewhere in 10-19 regardless of the units digit; the units digit can therefore be maximized to '9', giving hour "19". The minute's units digit is fixed at '9', and since minutes only range 00-59 (any tens digit 0-5 paired with units '9' stays valid), the tens digit can be maximized to '5', giving minute "59". The latest valid time is "19:59".
Example 2
Input
2?:5?
Expected
23:59
Explanation
The hour's tens digit is fixed at '2', which restricts the hour to 20-23, so the units digit can be at most '3' (choosing '9' would give the invalid hour "29"); the latest valid hour is "23". The minute's tens digit is fixed at '5', and any units digit 0-9 keeps the minute in the valid 50-59 range, so the units digit can be maximized to '9', giving minute "59". The latest valid time is "23:59".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →