diff --git a/2506_count_pair_of_similar_strings/Cargo.lock b/2506_count_pair_of_similar_strings/Cargo.lock new file mode 100644 index 0000000..e9bef01 --- /dev/null +++ b/2506_count_pair_of_similar_strings/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "count_pair_of_similar_strings" +version = "0.1.0" diff --git a/2506_count_pair_of_similar_strings/Cargo.toml b/2506_count_pair_of_similar_strings/Cargo.toml new file mode 100644 index 0000000..60523ac --- /dev/null +++ b/2506_count_pair_of_similar_strings/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "count_pair_of_similar_strings" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/2506_count_pair_of_similar_strings/src/lib.rs b/2506_count_pair_of_similar_strings/src/lib.rs new file mode 100644 index 0000000..7b9dc8d --- /dev/null +++ b/2506_count_pair_of_similar_strings/src/lib.rs @@ -0,0 +1,29 @@ +pub struct Solution; + +impl Solution { + pub fn similar_pairs(words: Vec) -> i32 { + let words: Vec = words + .iter() + .map(|word| Solution::convert_to_bits(word)) + .collect(); + + let mut result = 0; + for i in 0..words.len() { + for j in i + 1..words.len() { + if words[i] == words[j] { + result += 1; + } + } + } + + result + } + + fn convert_to_bits(word: &str) -> u32 { + let mut result = 0u32; + for c in word.chars() { + result |= 1 << c as u32 - 97; + } + result + } +}