#1736
Easy Algorithms Latest time by replacing hidden digits
String Greedy
43.6% acceptance
Feb 25, 2026
403
188
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.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn maximum_time(time: String) -> String {
let mut t: Vec<u8> = time.into_bytes();
// Position 0: tens of hours
if t[0] == b'?' {
t[0] = if t[1] == b'?' || t[1] <= b'3' { b'2' } else { b'1' };
}
// Position 1: units of hours
if t[1] == b'?' {
t[1] = if t[0] == b'2' { b'3' } else { b'9' };
}
// Position 3: tens of minutes
if t[3] == b'?' { t[3] = b'5'; }
// Position 4: units of minutes
if t[4] == b'?' { t[4] = b'9'; }
String::from_utf8(t).unwrap()
}
}