You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).
The valid times are those inclusively between 00:00 and 23:59.
Return the latest valid time you can get from time by replacing the hidden digits.
Example 1:
Input: time = "2?:?0" Output: "23:50" Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
Example 2:
Input: time = "0?:3?" Output: "09:39"
Example 3:
Input: time = "1?:22" Output: "19:22"
Constraints:
time is in the format hh:mm.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the latest possible time by filling in question marks with digits. Brute force means we will try out every possible digit for each question mark. We then check if each resulting time is valid and pick the latest one.
Here's how the algorithm would work step-by-step:
def latest_time_by_replacing_hidden_digits_brute_force(time):
latest_valid_time = ""
def is_valid_time(hour, minute):
return 0 <= hour <= 23 and 0 <= minute <= 59
def generate_times(index, current_time):
nonlocal latest_valid_time
if index == 5:
hour = int(current_time[0:2])
minute = int(current_time[3:5])
# Check if the generated time is valid.
if is_valid_time(hour, minute):
if latest_valid_time == "" or current_time > latest_valid_time:
latest_valid_time = current_time
return
if time[index] == '?':
for digit in range(10):
new_time = list(current_time)
new_time[index] = str(digit)
generate_times(index + 1, "".join(new_time))
else:
generate_times(index + 1, current_time)
# Handle the colon character without processing it as a digit
generate_times(0, list(time))
return latest_valid_timeThe goal is to find the latest possible time given a string with hidden digits. Instead of trying all combinations, we'll strategically fill in the blanks to maximize each digit from left to right, ensuring the resulting time is always valid.
Here's how the algorithm would work step-by-step:
def latest_time_by_replacing_hidden_digits(time):
time_list = list(time)
# Determine the first digit of the hour.
if time_list[0] == '?':
if time_list[1] == '?' or int(time_list[1]) < 4:
time_list[0] = '2'
else:
time_list[0] = '1'
# Determine the second digit of the hour.
if time_list[1] == '?':
if time_list[0] == '2':
time_list[1] = '3'
else:
time_list[1] = '9'
# The largest valid value for minutes first digit is 5.
if time_list[3] == '?':
time_list[3] = '5'
# Always maximize the second digit of the minutes.
if time_list[4] == '?':
time_list[4] = '9'
return "".join(time_list)| Case | How to Handle |
|---|---|
| Input string is null or empty | Return an appropriate error message or a default value like '00:00' based on problem constraints, after validating input. |
| Input string length is not 5 or format is incorrect (not 'HH:MM') | Validate the length and format of the input and return an error if incorrect. |
| All digits are hidden ('??:??') | The algorithm should correctly generate the maximum possible time '23:59' through its iterative replacement process. |
| Hour is partially defined (e.g., '?3:??') | The algorithm needs to intelligently fill the unknown hour digit, considering the existing one (in this case, the first digit must be either '0', '1', or '2'). |
| Minute is partially defined (e.g., '??:?9') | The algorithm should choose the maximum possible digit for the undefined part of the minute, respecting the constraints (in this case the first digit must be between '0' and '5'). |
| Input like '24:00' or '1?:??' | The algorithm should intelligently backtrack and try other valid numbers, such as changing the first question mark to '1' to make it '19:59' instead of failing. |
| Hour allows two values (e.g., '?3:??', becomes 23:??). Minutes become invalid (e.g. 23:6?). | Backtracking or constraints propogation is necessary to ensure a valid minute construction if hour selection initially leads to minute overbound. |
| No valid time can be constructed due to conflicting constraints. | The algorithm should return a specific error value or a default invalid time indication like 'invalid' or 'error'. |