2615. 等值距离和

This commit is contained in:
li_chx 2026-04-23 08:53:17 +08:00
parent 6074be16ce
commit 4ee2acd245
Signed by: li_chx
GPG Key ID: C7CF27EFA1E58BAC
1 changed files with 17 additions and 29 deletions

View File

@ -1,31 +1,25 @@
use crate::arr::make_string_arr; use std::collections::HashMap;
mod arr; mod arr;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
fn diff_count_less_two(a: &str, b: &str) -> bool { pub fn distance(nums: Vec<i32>) -> Vec<i64> {
let a = a.as_bytes(); let mut ans = vec![0_i64; nums.len()];
let b = b.as_bytes(); let mut map: HashMap<i32, Vec<usize>> = HashMap::new();
let mut cnt = 0; for (i, num) in nums.iter().enumerate() {
for i in 0..a.len() { map.entry(*num).or_insert(vec![]).push(i);
if a[i] != b[i] {
cnt += 1;
if cnt > 2 {
return false;
}
}
} }
true for (_, v) in map.into_iter() {
} let mut local_len = 0;
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> { for i in 1..v.len() {
let mut ans = vec![]; local_len += v[i] - v[0];
for query in queries { }
for dict in dictionary.iter() { ans[v[0]] = local_len as i64;
if Self::diff_count_less_two(&query, dict) { for i in 1..v.len() {
ans.push(query.clone()); local_len =
break; local_len - (v[i] - v[i - 1]) * (v.len() - 1 - i) + (v[i] - v[i - 1]) * (i - 1);
} ans[v[i]] = local_len as i64;
} }
} }
ans ans
@ -33,11 +27,5 @@ impl Solution {
} }
fn main() { fn main() {
println!( println!("{:?}", Solution::distance(vec![1, 3, 1, 1, 2]));
"{:?}",
Solution::two_edit_words(
make_string_arr(r#"["word","note","ants","wood"]"#),
make_string_arr(r#"["wood","joke","moat"]"#)
)
);
} }