forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0680-valid-palindrome-II.rs
More file actions
33 lines (30 loc) · 846 Bytes
/
0680-valid-palindrome-II.rs
File metadata and controls
33 lines (30 loc) · 846 Bytes
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
impl Solution {
pub fn valid_palindrome(s: String) -> bool {
fn is_palindrome(s: &[u8]) -> Option<(usize, usize)> {
let (mut i, mut j) = (0, s.len() - 1);
while i < j {
if s[i] != s[j] {
return Some((i, j));
}
i += 1;
j -= 1;
}
None // is palindrome
}
let s = s.as_bytes().to_vec();
match is_palindrome(&s) {
Some((i, j)) => {
if is_palindrome(&s[i+1..=j]).is_none() {
return true;
}
if is_palindrome(&s[i..=j-1]).is_none() {
return true;
}
}
None => {
return true;
}
}
false
}
}