pub(crate) fn edit_distance(a: &str, b: &str) -> usize {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
edit_distance_chars(&a, &b)
}
pub(crate) fn edit_distance_chars(a: &[char], b: &[char]) -> usize {
if a.is_empty() {
return b.len();
}
let mut previous: Vec<usize> = (0..=b.len()).collect();
let mut current = vec![0usize; b.len() + 1];
for (i, ca) in a.iter().enumerate() {
current[0] = i + 1;
for (j, cb) in b.iter().enumerate() {
let substitution = previous[j] + usize::from(ca != cb);
current[j + 1] = substitution.min(previous[j + 1] + 1).min(current[j] + 1);
}
std::mem::swap(&mut previous, &mut current);
}
previous[b.len()]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn edit_distance_counts_single_character_edits() {
assert_eq!(edit_distance("port", "port"), 0);
assert_eq!(edit_distance("portt", "port"), 1);
assert_eq!(edit_distance("prot", "port"), 2);
assert_eq!(edit_distance("", "port"), 4);
assert_eq!(edit_distance("port", ""), 4);
}
#[test]
fn the_chars_form_agrees_with_the_str_form() {
for (a, b) in [("http_call", "http_cal"), ("map", "x"), ("", "")] {
let (ac, bc): (Vec<char>, Vec<char>) = (a.chars().collect(), b.chars().collect());
assert_eq!(edit_distance_chars(&ac, &bc), edit_distance(a, b));
}
}
}