pub fn levenshtein_distance(str1: &str, str2: &str) -> usize {
    let m = str1.len();
    let n = str2.len();

    // Test for empty strings.
    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }

    let mut v0 = vec![0; n];
    let mut v1 = vec![0; n];

    let str1_bytes = str1.as_bytes();
    let str2_bytes = str2.as_bytes();

    for i in 0..n {
        v0[i] = i;
    }
    for i in 0..m-1 {
        v1[0] = i + 1;
        
        for j in 0..n-1 {
            let mut costs = vec![0; 3]; // deletion, insertion, and substitution costs (in that order)
            costs[0] = v0[j + 1] + 1;
            costs[1] = v1[j] + 1;
            if str1_bytes[i] == str2_bytes[j] {
                costs[2] = v0[j];
            }
            else {
                costs[2] = v0[j] + 1;
            }
            v1[j + 1] = statistics::min_usize(&costs);
        }

        // Copy v1 to v0.
        let temp_v = v0.clone();
        v0 = v1.clone();
        v1 = temp_v.clone();
    }

    // Save the final distance calculation, before freeing the vector.
    let distance = v0[n - 1];
    distance
}
/// Computes the Levenshtein distance between the two strings.
Computes the Levenshtein distance between the two strings.
Computes the Levenshtein distance between the two strings .
pub fn levenshtein_distance(str1: &str, str2: &str) -> usize {
    let m = str1.len();
    let n = str2.len();
    // Test for empty strings.
    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }
    let mut v0 = vec![0; n];
    let mut v1 = vec![0; n];
    let str1_bytes = str1.as_bytes();
    let str2_bytes = str2.as_bytes();
    for i in 0..n {
        v0[i] = i;
    }
    for i in 0..m-1 {
        v1[0] = i + 1;
        for j in 0..n-1 {
            let mut costs = vec![0; 3]; // deletion, insertion, and substitution costs (in that order)
            costs[0] = v0[j + 1] + 1;
            costs[1] = v1[j] + 1;
            if str1_bytes[i] == str2_bytes[j] {
                costs[2] = v0[j];
            }
            else {
                costs[2] = v0[j] + 1;
            }
            v1[j + 1] = statistics::min_usize(&costs);
        }
        // Copy v1 to v0.
        let temp_v = v0.clone();
        v0 = v1.clone();
        v1 = temp_v.clone();
    }
    // Save the final distance calculation, before freeing the vector.
    let distance = v0[n - 1];
    distance
}
pub fn levenshtein_distance ( str1 : & str , str2 : & str ) -> usize { let m = str1 . len ( ) ; let n = str2 . len ( ) ; // Test for empty strings. if m == 0 { return n ; } if n == 0 { return m ; } let mut v0 = vec ! [ 0 n ] ; let mut v1 = vec ! [ 0 n ] ; let str1_bytes = str1 . as_bytes ( ) ; let str2_bytes = str2 . as_bytes ( ) ; for i in 0 .. n { v0 [ i ] = i ; } for i in 0 .. m - 1 { v1 [ 0 ] = i + 1 ; for j in 0 .. n - 1 { let mut costs = vec ! [ 0 3 ] ; // deletion, insertion, and substitution costs (in that order) costs [ 0 ] = v0 [ j + 1 ] + 1 ; costs [ 1 ] = v1 [ j ] + 1 ; if str1_bytes [ i ] == str2_bytes [ j ] { costs [ 2 ] = v0 [ j ] ; } else { costs [ 2 ] = v0 [ j ] + 1 ; } v1 [ j + 1 ] = statistics :: min_usize ( & costs ) ; } // Copy v1 to v0. let temp_v = v0 . clone ( ) ; v0 = v1 . clone ( ) ; v1 = temp_v . clone ( ) ; } // Save the final distance calculation, before freeing the vector. let distance = v0 [ n - 1 ] ; distance }
Computes the Levenshtein distance between the two strings.
Computes the Levenshtein distance between the two strings .
str1
&str
str2
&str