Skip to content

Commit

Permalink
Add problem 1980: Find Unique Binary String
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 1, 2023
1 parent c5e48cf commit d512ced
Show file tree
Hide file tree
Showing 3 changed files with 53 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 @@ -1373,6 +1373,7 @@ pub mod problem_1971_find_if_path_exists_in_graph;
pub mod problem_1974_minimum_time_to_type_word_using_special_typewriter;
pub mod problem_1975_maximum_matrix_sum;
pub mod problem_1979_find_greatest_common_divisor_of_array;
pub mod problem_1980_find_unique_binary_string;

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

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

impl Solution {
pub fn find_different_binary_string(nums: Vec<String>) -> String {
String::from_utf8(
nums.into_iter()
.enumerate()
.map(|(i, s)| b'0' + b'1' - s.as_bytes()[i])
.collect(),
)
.unwrap()
}
}

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

impl super::Solution for Solution {
fn find_different_binary_string(nums: Vec<String>) -> String {
Self::find_different_binary_string(nums)
}
}

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

pub trait Solution {
fn find_different_binary_string(nums: Vec<String>) -> String;
}

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

pub fn run<S: Solution>() {
let test_cases = [&["01", "10"] as &[_], &["00", "01"], &["111", "011", "001"]];

for nums in test_cases {
let result = S::find_different_binary_string(nums.iter().copied().map(str::to_string).collect());

assert_eq!(result.len(), nums[0].len());
assert!(!nums.contains(&result.as_str()));
}
}
}

0 comments on commit d512ced

Please sign in to comment.