diff options
| author | Kang-min Liu <gugod@gugod.org> | 2021-01-21 08:05:31 +0900 |
|---|---|---|
| committer | Kang-min Liu <gugod@gugod.org> | 2021-01-21 08:05:31 +0900 |
| commit | 2423900c332638e4081df3956e73479cfee65df8 (patch) | |
| tree | 47b99093acd03e7a4713d8b26eee904c0dde71ac | |
| parent | 34531261097ff884893213a0dd4dea05fa8533bd (diff) | |
| download | perlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.tar.gz perlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.tar.bz2 perlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.zip | |
a solution to pwc 096.2 in rust
| -rw-r--r-- | challenge-096/gugod/rust/ch-2.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-096/gugod/rust/ch-2.rs b/challenge-096/gugod/rust/ch-2.rs new file mode 100644 index 0000000000..7a6761191a --- /dev/null +++ b/challenge-096/gugod/rust/ch-2.rs @@ -0,0 +1,35 @@ + +fn main() { + let examples: [[&str; 2]; 2] = [ + ["kitten", "sitting"], + ["sunday", "monday"], + ]; + for x in examples.iter() { + println!("Input: S1={}, S2={}", x[0], x[1]); + println!("Output: {}", edit_distance(x[0], x[1])); + } +} + +fn edit_distance(a: &str, b: &str) -> u32 { + let chars_a = a.chars().collect(); + let chars_b = b.chars().collect(); + return lev( &chars_a, 0, &chars_b, 0); +} + +fn lev (a: &Vec<char>, offset_a: usize, b: &Vec<char>, offset_b: usize) -> u32 { + if offset_a == a.len() { + return (b.len() - offset_b) as u32; + } + if offset_b == b.len() { + return (a.len() - offset_a) as u32; + } + + let n3 = lev( a, offset_a+1, b, offset_b+1 ); + if a[offset_a] == b[offset_b] { + return n3; + } else { + let n1 = lev( a, offset_a+1, b, offset_b ); + let n2 = lev( a, offset_a, b, offset_b+1 ); + return [n1, n2, n3].iter().min().unwrap() + 1_u32; + } +} |
