-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-away-lcci.rs
63 lines (58 loc) · 1.41 KB
/
one-away-lcci.rs
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
61
62
63
struct Solution;
impl Solution {
pub fn one_edit_away(first: String, second: String) -> bool {
if first == second {
return true;
}
let mut num = 0;
let (mut f, mut s) = (
first.chars().collect::<Vec<char>>(),
second.chars().collect::<Vec<char>>(),
);
if f.len() == s.len() {
for i in 0..f.len() {
if f[i] != s[i] {
num += 1;
}
if num >= 2 {
return false;
}
}
true
} else {
if f.len() < s.len() {
std::mem::swap(&mut f, &mut s);
}
for i in 0..f.len() {
let mut ans: Vec<char> = vec![];
ans.extend(&f[..i]);
ans.extend(&f[i + 1..]);
if ans == s {
return true;
}
}
false
}
}
}
fn main() {
println!(
"{}",
Solution::one_edit_away("pale".to_string(), "ple".to_string())
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert!(Solution::one_edit_away(
"pale".to_string(),
"ple".to_string()
));
assert!(!Solution::one_edit_away(
"pales".to_string(),
"pal".to_string()
));
}
}