Skip to content

Commit 1ca342a

Browse files
committed
Add problem 3114: Latest Time You Can Obtain After Replacing Characters
1 parent 1dea942 commit 1ca342a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ pub mod problem_3106_lexicographically_smallest_string_after_operations_with_con
20932093
pub mod problem_3107_minimum_operations_to_make_median_of_array_equal_to_k;
20942094
pub mod problem_3110_score_of_a_string;
20952095
pub mod problem_3111_minimum_rectangles_to_cover_points;
2096+
pub mod problem_3114_latest_time_you_can_obtain_after_replacing_characters;
20962097
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20972098

20982099
#[cfg(test)]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn find_latest_time(s: String) -> String {
7+
let mut s = s.into_bytes();
8+
let [h0, h1, _, m0, m1] = <&mut [_; 5]>::try_from(s.as_mut_slice()).ok().unwrap();
9+
10+
if *h0 == b'?' {
11+
if *h1 == b'?' {
12+
*h0 = b'1';
13+
*h1 = b'1';
14+
} else {
15+
*h0 = b'0' + u8::from(*h1 < b'2');
16+
}
17+
} else if *h1 == b'?' {
18+
*h1 = b'9' - (*h0 - b'0') * 8;
19+
}
20+
21+
if m0 == &b'?' {
22+
*m0 = b'5';
23+
}
24+
25+
if m1 == &b'?' {
26+
*m1 = b'9';
27+
}
28+
29+
String::from_utf8(s).ok().unwrap()
30+
}
31+
}
32+
33+
// ------------------------------------------------------ snip ------------------------------------------------------ //
34+
35+
impl super::Solution for Solution {
36+
fn find_latest_time(s: String) -> String {
37+
Self::find_latest_time(s)
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
#[test]
44+
fn test_solution() {
45+
super::super::tests::run::<super::Solution>();
46+
}
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn find_latest_time(s: String) -> String;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("1?:?4", "11:54"), ("0?:5?", "09:59")];
13+
14+
for (s, expected) in test_cases {
15+
assert_eq!(S::find_latest_time(s.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)