From 5ece9ce221d408506bf0a6c8398b6a2085656cb7 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 22 Feb 2025 11:40:57 +0800 Subject: [PATCH] 20250222 finished. --- src/problem/mod.rs | 2 + .../p2506_count_pairs_of_similar_strings.rs | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/problem/p2506_count_pairs_of_similar_strings.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 1b66ce5..f12de11 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -498,3 +498,5 @@ mod p624_maximum_distance_in_arrays; mod p2595_number_of_even_and_odd_bits; mod p2209_minimum_white_tiles_after_covering_with_carpets; + +mod p2506_count_pairs_of_similar_strings; diff --git a/src/problem/p2506_count_pairs_of_similar_strings.rs b/src/problem/p2506_count_pairs_of_similar_strings.rs new file mode 100644 index 0000000..029cb20 --- /dev/null +++ b/src/problem/p2506_count_pairs_of_similar_strings.rs @@ -0,0 +1,50 @@ +/** + * [2506] Count Pairs Of Similar Strings + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn similar_pairs(words: Vec) -> i32 { + let mut map = HashMap::new(); + + for word in words.iter() { + let mut bits = 0; + + for c in word.chars() { + bits = bits | (1 << (c as u8 - 'a' as u8)) + } + + let entry = map.entry(bits).or_insert(0); + *entry += 1; + } + + let mut result = 0; + + for &v in map.values() { + // C(n, 2) + result += v * (v - 1) / 2; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2506() { + assert_eq!( + 2, + Solution::similar_pairs(vec_string!("aba", "aabb", "abcd", "bac", "aabc")) + ); + assert_eq!(3, Solution::similar_pairs(vec_string!("aabb", "ab", "ba"))); + assert_eq!(0, Solution::similar_pairs(vec_string!("nba", "cba", "dba"))); + } +}