A salvage crew has raised an antique ship's chronometer from a wreck. Its display always shows a five-character readout in the fixed layout HH:MM, giving a time on a 24-hour clock: HH runs from 00 to 23 and MM runs from 00 to 59. Decades underwater have corroded away some of the display's digits, and every corroded position now shows a ? instead of its original digit. Determine how many different original times are consistent with the corroded readout — that is, in how many ways can every ? be replaced by a digit 0-9 so that the resulting five characters form a valid HH:MM time.
A single line containing the five-character readout s. The character at index 2 (0-indexed) is always :. Each of the remaining four characters, at indices 0, 1, 3 and 4, is either a decimal digit 0-9 or the character ?.
Print a single integer: the number of ways to replace the ? characters with digits so that the resulting string is a valid HH:MM time (00 <= HH <= 23, 00 <= MM <= 59). Print 0 if no replacement yields a valid time.
len(s) == 5s[2] == ':'s[0], s[1], s[3], s[4] is either a digit '0'-'9' or '?'.Example 1
Input
?5:00
Expected
2
Explanation
The hour's tens digit is unknown and its ones digit is fixed at 5, so the hour can be 05 or 15 (a tens digit of 2 would give 25, which is out of range); the minute is fully fixed at 00, which is valid. That gives 2 * 1 = 2 valid times: 05:00 and 15:00.
Example 2
Input
23:5?
Expected
10
Explanation
The hour is fully fixed at 23, which is a valid hour. The minute's tens digit is fixed at 5 and its ones digit is unknown, so the minute can be any of 50 through 59 — all 10 are valid minutes. That gives 1 * 10 = 10 valid times.
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 →