Skip to content

Commit

Permalink
Add problem 2027: Minimum Moves to Convert String
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 25, 2023
1 parent afcabb2 commit e02ddf8
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -1397,6 +1397,7 @@ pub mod problem_2011_final_value_of_variable_after_performing_operations;
pub mod problem_2012_sum_of_beauty_in_the_array;
pub mod problem_2016_maximum_difference_between_increasing_elements;
pub mod problem_2022_find_original_array_from_doubled_array;
pub mod problem_2027_minimum_moves_to_convert_string;

#[cfg(test)]
mod test_utilities;
37 changes: 37 additions & 0 deletions src/problem_2027_minimum_moves_to_convert_string/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn minimum_moves(s: String) -> i32 {
let mut result = 0;
let mut i = 0;

while let Some(&c) = s.as_bytes().get(i) {
if c == b'O' {
i += 1;
} else {
result += 1;
i += 3;
}
}

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn minimum_moves(s: String) -> i32 {
Self::minimum_moves(s)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_2027_minimum_moves_to_convert_string/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn minimum_moves(s: String) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("XXX", 1), ("XXOX", 2), ("OOOO", 0)];

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

0 comments on commit e02ddf8

Please sign in to comment.