Skip to content

Commit 93b9054

Browse files
committed
Add problem 2744: Find Maximum Number of String Pairs
1 parent 385ba2b commit 93b9054

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ pub mod problem_2733_neither_minimum_nor_maximum;
19571957
pub mod problem_2734_lexicographically_smallest_string_after_substring_operation;
19581958
pub mod problem_2739_total_distance_traveled;
19591959
pub mod problem_2740_find_the_value_of_the_partition;
1960+
pub mod problem_2744_find_maximum_number_of_string_pairs;
19601961

19611962
#[cfg(test)]
19621963
mod test_utilities;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::collections::HashSet;
6+
7+
impl Solution {
8+
pub fn maximum_number_of_string_pairs(words: Vec<String>) -> i32 {
9+
let mut reversed = HashSet::new();
10+
let mut result = 0;
11+
12+
for word in words {
13+
let mut word = word.into_bytes();
14+
15+
if reversed.remove(word.as_slice()) {
16+
result += 1;
17+
} else {
18+
word.reverse();
19+
reversed.insert(word);
20+
}
21+
}
22+
23+
result
24+
}
25+
}
26+
27+
// ------------------------------------------------------ snip ------------------------------------------------------ //
28+
29+
impl super::Solution for Solution {
30+
fn maximum_number_of_string_pairs(words: Vec<String>) -> i32 {
31+
Self::maximum_number_of_string_pairs(words)
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
#[test]
38+
fn test_solution() {
39+
super::super::tests::run::<super::Solution>();
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod hash_set;
2+
3+
pub trait Solution {
4+
fn maximum_number_of_string_pairs(words: Vec<String>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(&["cd", "ac", "dc", "ca", "zz"] as &[_], 2),
14+
(&["ab", "ba", "cc"], 1),
15+
(&["aa", "ab"], 0),
16+
];
17+
18+
for (words, expected) in test_cases {
19+
assert_eq!(
20+
S::maximum_number_of_string_pairs(words.iter().copied().map(str::to_string).collect()),
21+
expected,
22+
);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)