feat: 2506_count_pair_of_similar_strings
This commit is contained in:
parent
40f97f8941
commit
58f8bad404
7
2506_count_pair_of_similar_strings/Cargo.lock
generated
Normal file
7
2506_count_pair_of_similar_strings/Cargo.lock
generated
Normal 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"
|
6
2506_count_pair_of_similar_strings/Cargo.toml
Normal file
6
2506_count_pair_of_similar_strings/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "count_pair_of_similar_strings"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
29
2506_count_pair_of_similar_strings/src/lib.rs
Normal file
29
2506_count_pair_of_similar_strings/src/lib.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user