-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0072_edit_distance.rs
More file actions
60 lines (48 loc) · 1.3 KB
/
s0072_edit_distance.rs
File metadata and controls
60 lines (48 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![allow(unused)]
pub struct Solution {}
use std::cmp::min;
use std::i32::MAX;
impl Solution {
pub fn min_distance(word1: String, word2: String) -> i32 {
let (n, m) = (word1.len(), word2.len());
if n * m == 0 {
return (n + m) as i32;
}
let mut dp = vec![vec![MAX; m + 1]; n + 1];
for i in 0..=n {
dp[i][0] = i as i32;
}
for j in 0..=m {
dp[0][j] = j as i32;
}
let wd1 = word1.chars().collect::<Vec<char>>();
let wd2 = word2.chars().collect::<Vec<char>>();
// dp compute
for i in 1..=n {
for j in 1..=m {
let (left, down, mut left_down) =
(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1]);
if wd1[i - 1] != wd2[j - 1] {
left_down += 1;
}
dp[i][j] = min(left, min(down, left_down));
}
}
dp[n][m]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_72() {
assert_eq!(
Solution::min_distance("horse".to_owned(), "ros".to_owned()),
3
);
assert_eq!(
Solution::min_distance("intention".to_owned(), "execution".to_owned()),
5
);
}
}