feat: 2506_count_pair_of_similar_strings

This commit is contained in:
SquidSpirit 2025-08-21 00:26:35 +08:00
parent 40f97f8941
commit 58f8bad404
3 changed files with 42 additions and 0 deletions

View File

@ -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"

View File

@ -0,0 +1,6 @@
[package]
name = "count_pair_of_similar_strings"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,29 @@
pub struct Solution;
impl Solution {
pub fn similar_pairs(words: Vec<String>) -> i32 {
let words: Vec<u32> = 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
}
}