Skip to content

Commit

Permalink
Add problem 2375: Construct Smallest Number From DI String
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 1, 2025
1 parent 94c5e8f commit 2d8802f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
19 changes: 19 additions & 0 deletions src/problem_2375_construct_smallest_number_from_di_string/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
let test_cases = [("IIIDIDDD", "123549876"), ("DDD", "4321"), ("DDDIII", "4321567")];

for (pattern, expected) in test_cases {
assert_eq!(S::smallest_number(pattern.to_string()), expected);
}
}
}

0 comments on commit 2d8802f

Please sign in to comment.