A factory's mechanical time clock stamps each worker's card with the current time in 12-hour format "HH:MM AM" or "HH:MM PM" (the hour is always printed with a leading zero, from "01" to "12"). After years of use, the ink ribbon has faded unevenly, so some of the printed characters — hour digits, minute digits, or the two letters of the AM/PM mark — have become illegible and are recorded as '?'. Every character that is still legible is guaranteed correct. Reconstruct the latest possible moment of the 12-hour cycle (treating "12:00 AM", i.e. midnight, as the earliest instant of the cycle and "11:59 PM" as the latest) that is consistent with every legible character, and print the reconstructed stamp in the same format.
A single line containing a string of exactly 8 characters in the form "HH:MM AP", where:
A single line containing the reconstructed stamp, in the identical "HH:MM AP" format, representing the latest valid moment of the 12-hour cycle consistent with the input.
Example 1
Input
1?:?? ?M
Expected
11:59 PM
Explanation
The hour pattern "1?" restricted to a valid 1-12 hour (leading zero form) allows only "10", "11", or "12" (since "13" through "19" aren't valid 12-hour hours). The minute is fully free, so it can be maximized to "59". The meridiem pattern "?M" allows both "AM" and "PM". Comparing chronologically: among {10,11,12} paired with PM, the hour-of-day values are 22, 23, and 12 (noon) respectively — so "11 PM" (23) beats even "12 PM" (12, noon). Among {10,11,12} paired with AM the best is "11 AM" (11). PM wins overall, and within PM the best hour is 11. The latest valid stamp is "11:59 PM".
Example 2
Input
?2:00 AM
Expected
02:00 AM
Explanation
The hour pattern "?2" restricted to a valid 1-12 hour allows only "02" or "12" (values 2 and 12; a tens digit of 2-9 with units '2' would exceed 12). The minute is fixed at "00" and the meridiem is fixed at "AM". Converting to hour-of-day: "02" with AM gives 2, while "12" with AM gives 0 (midnight, the very earliest instant of the whole cycle). So hour "02" is later than hour "12" here, even though "12" is numerically the larger printed hour. The latest valid stamp is "02:00 AM".
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 →