You are given a string time
in the format HH:MM
representing the latest possible time you could produce by changing at most 2 characters from the initial time
. Return the latest possible time you can get.
For example:
time = "2?:?0"
, you can replace the ?
with digits to produce the latest possible time, which is “23:50”.time = "0?:3?"
, the latest possible time is “09:39”.HH:MM
?
:
and all non-?
characters valid digits for a time?
?
characters will be valid digits.time[0]
), determine if it’s ?
. If it is, consider the constraints based on the second character (time[1]
).
time[1]
is between 0-3
or ?
, set time[0]
to 2
.time[0]
to 1
.time[1]
), if it’s ?
:
time[0]
is 2
, set it to 3
.9
.:
and does not need modification.time[3]
), if it’s ?
, set it to 5
(latest possible for tens of minutes).time[4]
), if it’s ?
, set it to 9
(latest possible for units of minutes).def latestTime(time: str) -> str:
# Convert string to a list of characters for easier manipulation
time = list(time)
# Determine the maximum hour
if time[0] == '?':
if time[1] == '?' or time[1] < '4':
time[0] = '2'
else:
time[0] = '1'
if time[1] == '?':
if time[0] == '2':
time[1] = '3'
else:
time[1] = '9'
# The ':' character does not change, so time[2] is untouched
# Determine the maximum minute for tens place
if time[3] == '?':
time[3] = '5'
# Determine the maximum minute for units place
if time[4] == '?':
time[4] = '9'
# Convert list of characters back to string
return ''.join(time)
# Example usage
print(latestTime("2?:?0")) # Output: "23:50"
print(latestTime("0?:3?")) # Output: "09:39"
print(latestTime("1?:22")) # Output: "19:22"
print(latestTime("?4:5?")) # Output: "14:59"
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?