diff --git a/src/lib.rs b/src/lib.rs index 3cb00561..05177578 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1768,6 +1768,7 @@ pub mod problem_2369_check_if_there_is_a_valid_partition_for_the_array; pub mod problem_2370_longest_ideal_subsequence; pub mod problem_2373_largest_local_values_in_a_matrix; pub mod problem_2374_node_with_highest_edge_score; +pub mod problem_2375_construct_smallest_number_from_di_string; pub mod problem_2379_minimum_recolors_to_get_k_consecutive_black_blocks; pub mod problem_2380_time_needed_to_rearrange_a_binary_string; pub mod problem_2381_shifting_letters_ii; diff --git a/src/problem_2375_construct_smallest_number_from_di_string/greedy.rs b/src/problem_2375_construct_smallest_number_from_di_string/greedy.rs new file mode 100644 index 00000000..3121e535 --- /dev/null +++ b/src/problem_2375_construct_smallest_number_from_di_string/greedy.rs @@ -0,0 +1,59 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn smallest_number(pattern: String) -> String { + let n = pattern.len(); + let mut result = String::with_capacity(n + 1); + let mut prev = b'I'; + let mut length = 0; + let mut c = b'1'; + + for direction in pattern.bytes() { + if direction == prev { + length += 1; + } else { + if prev == b'I' { + result.extend((c - length..c).map(char::from)); + length = 1; + } else { + result.extend((c - length..=c).map(char::from).rev()); + length = 0; + } + + prev = direction; + } + + c += 1; + } + + drop(pattern); + + let iter = (c - length..=c).map(char::from); + + if prev == b'I' { + result.extend(iter); + } else { + result.extend(iter.rev()); + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn smallest_number(pattern: String) -> String { + Self::smallest_number(pattern) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2375_construct_smallest_number_from_di_string/greedy_2.rs b/src/problem_2375_construct_smallest_number_from_di_string/greedy_2.rs new file mode 100644 index 00000000..04aad572 --- /dev/null +++ b/src/problem_2375_construct_smallest_number_from_di_string/greedy_2.rs @@ -0,0 +1,69 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn smallest_number(pattern: String) -> String { + let n = pattern.len(); + let mut result = String::with_capacity(n + 1); + let mut length = 0; + let mut c = b'1'; + let mut iter = pattern.bytes(); + + 'outer: loop { + if let Some(direction) = iter.next() { + if direction == b'I' { + length += 1; + c += 1; + } else { + result.extend((c - length..c).map(char::from)); + + length = 2; + c += 2; + + loop { + if let Some(direction) = iter.next() { + if direction == b'I' { + break; + } + + length += 1; + c += 1; + } else { + result.extend((c - length..c).map(char::from).rev()); + + break 'outer; + } + } + + result.extend((c - length..c).map(char::from).rev()); + length = 0; + } + } else { + result.extend((c - length..=c).map(char::from)); + + break; + } + } + + drop(pattern); + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn smallest_number(pattern: String) -> String { + Self::smallest_number(pattern) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2375_construct_smallest_number_from_di_string/mod.rs b/src/problem_2375_construct_smallest_number_from_di_string/mod.rs new file mode 100644 index 00000000..be851b26 --- /dev/null +++ b/src/problem_2375_construct_smallest_number_from_di_string/mod.rs @@ -0,0 +1,19 @@ +pub mod greedy; +pub mod greedy_2; + +pub trait Solution { + fn smallest_number(pattern: String) -> String; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("IIIDIDDD", "123549876"), ("DDD", "4321"), ("DDDIII", "4321567")]; + + for (pattern, expected) in test_cases { + assert_eq!(S::smallest_number(pattern.to_string()), expected); + } + } +}