The "Latest Time by Replacing Hidden Digits" problem gives you a string time
representing a time in the format "hh:mm"
. Some digits in the string are replaced with the character '?'
. Your task is to replace every '?'
with a digit (0-9) to make the latest valid 24-hour time possible.
'?'
must be replaced with a single digit.00:00
to 23:59
).'?'
in place.
For example, given time = "?4:5?"
, your goal is to replace both '?'
so the resulting time is as late as possible and still valid.
The problem requires maximizing the time, so we want to choose the largest possible digit for each '?'
. However, constraints for valid hours (00
to 23
) and minutes (00
to 59
) limit which digits are allowed in each position.
hh
), the first digit can be at most 2
, but if it's 2
, the second digit can only be 0-3
.
mm
), the first digit can be at most 5
.
'?'
, we pick the largest digit possible that keeps the time valid.
A brute-force approach would try all possible digit combinations, but that's unnecessary. Instead, by analyzing each position independently, we can decide the best digit for each '?'
based on the adjacent digits.
Let's break down the algorithm step-by-step:
time
string.
time[0]
) is '?'
:
time[1]
) is '?'
:
time[3]
) is '?'
, use '5' (so the minute can be 59).
time[4]
) is '?'
, use '9'.
'?'
with the appropriate digits, return the new string.
This approach is efficient because it only requires a single pass and simple logic at each character.
Let's use the input time = "?4:5?"
as an example:
'?'
(hour tens place).
'?'
with '1'.'?'
(minute ones place).
The result is "14:59"
, which is the latest valid time possible by replacing the hidden digits.
'?'
(potentially up to 10n for n question marks), but this is unnecessary and inefficient.
The key to solving "Latest Time by Replacing Hidden Digits" is understanding the constraints of each digit in the 24-hour time format. By making greedy choices—always selecting the largest valid digit for each '?'
—we efficiently construct the latest possible valid time. This method is both simple and optimal, requiring only a single scan through the string and constant space.
class Solution:
def maximumTime(self, time: str) -> str:
res = list(time)
# Hour tens
if res[0] == '?':
if res[1] == '?' or res[1] <= '3':
res[0] = '2'
else:
res[0] = '1'
# Hour ones
if res[1] == '?':
if res[0] == '2':
res[1] = '3'
else:
res[1] = '9'
# Minute tens
if res[3] == '?':
res[3] = '5'
# Minute ones
if res[4] == '?':
res[4] = '9'
return ''.join(res)
class Solution {
public:
string maximumTime(string time) {
string res = time;
// Hour tens
if (res[0] == '?') {
if (res[1] == '?' || res[1] <= '3') res[0] = '2';
else res[0] = '1';
}
// Hour ones
if (res[1] == '?') {
if (res[0] == '2') res[1] = '3';
else res[1] = '9';
}
// Minute tens
if (res[3] == '?') res[3] = '5';
// Minute ones
if (res[4] == '?') res[4] = '9';
return res;
}
};
class Solution {
public String maximumTime(String time) {
char[] res = time.toCharArray();
// Hour tens
if (res[0] == '?') {
if (res[1] == '?' || res[1] <= '3') res[0] = '2';
else res[0] = '1';
}
// Hour ones
if (res[1] == '?') {
if (res[0] == '2') res[1] = '3';
else res[1] = '9';
}
// Minute tens
if (res[3] == '?') res[3] = '5';
// Minute ones
if (res[4] == '?') res[4] = '9';
return new String(res);
}
}
var maximumTime = function(time) {
let res = time.split('');
// Hour tens
if (res[0] === '?') {
if (res[1] === '?' || res[1] <= '3') res[0] = '2';
else res[0] = '1';
}
// Hour ones
if (res[1] === '?') {
if (res[0] === '2') res[1] = '3';
else res[1] = '9';
}
// Minute tens
if (res[3] === '?') res[3] = '5';
// Minute ones
if (res[4] === '?') res[4] = '9';
return res.join('');
};