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
.struct Solution;
impl Solution {
fn maximum_time(time: String) -> String {
let time: Vec<char> = time.chars().collect();
let mut max = 0;
for i in 0..24 {
'outer: for j in 0..60 {
let t = i * 60 + j;
let s = format!("{:02}:{:02}", i, j);
let s: Vec<char> = s.chars().collect();
for i in 0..5 {
if time[i] != '?' && time[i] != s[i] {
continue 'outer;
}
}
max = max.max(t);
}
}
format!("{:02}:{:02}", max / 60, max % 60)
}
}
#[test]
fn test() {
let time = "2?:?0".to_string();
let res = "23:50".to_string();
assert_eq!(Solution::maximum_time(time), res);
let time = "0?:3?".to_string();
let res = "09:39".to_string();
assert_eq!(Solution::maximum_time(time), res);
let time = "1?:22".to_string();
let res = "19:22".to_string();
assert_eq!(Solution::maximum_time(time), res);
}