diff --git a/src/lib.rs b/src/lib.rs index f127b44b..a9d31011 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1365,6 +1365,7 @@ pub mod problem_1962_remove_stones_to_minimize_the_total; pub mod problem_1963_minimum_number_of_swaps_to_make_the_string_balanced; pub mod problem_1967_number_of_strings_that_appear_as_substrings_in_word; pub mod problem_1968_array_with_elements_not_equal_to_average_of_neighbors; +pub mod problem_1971_find_if_path_exists_in_graph; #[cfg(test)] mod test_utilities; diff --git a/src/problem_1971_find_if_path_exists_in_graph/iterative.rs b/src/problem_1971_find_if_path_exists_in_graph/iterative.rs new file mode 100644 index 00000000..1e64ced2 --- /dev/null +++ b/src/problem_1971_find_if_path_exists_in_graph/iterative.rs @@ -0,0 +1,36 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn min_time_to_type(word: String) -> i32 { + let mut current = b'a'; + let mut result = word.len() as u32; + + for c in word.bytes() { + let (min, max) = if c < current { (c, current) } else { (current, c) }; + + result += u32::from((max - min).min(min + 26 - max)); + + current = c; + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_time_to_type(word: String) -> i32 { + Self::min_time_to_type(word) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1971_find_if_path_exists_in_graph/mod.rs b/src/problem_1971_find_if_path_exists_in_graph/mod.rs new file mode 100644 index 00000000..d29c65b3 --- /dev/null +++ b/src/problem_1971_find_if_path_exists_in_graph/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn min_time_to_type(word: String) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("abc", 5), ("bza", 7), ("zjpc", 34)]; + + for (word, expected) in test_cases { + assert_eq!(S::min_time_to_type(word.to_string()), expected); + } + } +}