Skip to content

Commit a6cb3ca

Browse files
committed
Add problem 2839: Check if Strings Can be Made Equal With Operations I
1 parent 07e8e50 commit a6cb3ca

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ pub mod problem_2828_check_if_a_string_is_an_acronym_of_words;
19851985
pub mod problem_2829_determine_the_minimum_sum_of_a_k_avoiding_array;
19861986
pub mod problem_2830_maximize_the_profit_as_the_salesman;
19871987
pub mod problem_2833_furthest_point_from_origin;
1988+
pub mod problem_2839_check_if_strings_can_be_made_equal_with_operations_i;
19881989

19891990
#[cfg(test)]
19901991
mod test_utilities;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod sorting;
2+
3+
pub trait Solution {
4+
fn can_be_equal(s1: String, s2: String) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(("abcd", "cdab"), true), (("abcd", "dacb"), false)];
13+
14+
for ((s1, s2), expected) in test_cases {
15+
assert_eq!(S::can_be_equal(s1.to_string(), s2.to_string()), expected);
16+
}
17+
}
18+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::mem;
6+
7+
impl Solution {
8+
fn sort(value: &mut [u8; 4]) {
9+
let [a, b, c, d] = value;
10+
11+
if *c < *a {
12+
mem::swap(a, c);
13+
}
14+
15+
if *d < *b {
16+
mem::swap(b, d);
17+
}
18+
}
19+
20+
pub fn can_be_equal(s1: String, s2: String) -> bool {
21+
let mut s1 = s1.into_bytes();
22+
let s1 = <&mut [_; 4]>::try_from(s1.as_mut_slice()).ok().unwrap();
23+
let mut s2 = s2.into_bytes();
24+
let s2 = <&mut [_; 4]>::try_from(s2.as_mut_slice()).ok().unwrap();
25+
26+
Self::sort(s1);
27+
Self::sort(s2);
28+
29+
s1 == s2
30+
}
31+
}
32+
33+
// ------------------------------------------------------ snip ------------------------------------------------------ //
34+
35+
impl super::Solution for Solution {
36+
fn can_be_equal(s1: String, s2: String) -> bool {
37+
Self::can_be_equal(s1, s2)
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
#[test]
44+
fn test_solution() {
45+
super::super::tests::run::<super::Solution>();
46+
}
47+
}

0 commit comments

Comments
 (0)