Skip to content

Commit 203eed4

Browse files
committed
0680
1 parent ee7a3bc commit 203eed4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: rust/0680-valid-palindrome-II.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn valid_palindrome(s: String) -> bool {
3+
fn is_palindrome(s: &[u8]) -> Option<(usize, usize)> {
4+
let (mut i, mut j) = (0, s.len() - 1);
5+
while i < j {
6+
if s[i] != s[j] {
7+
return Some((i, j));
8+
}
9+
i += 1;
10+
j -= 1;
11+
}
12+
13+
None // is palindrome
14+
}
15+
16+
let s = s.as_bytes().to_vec();
17+
match is_palindrome(&s) {
18+
Some((i, j)) => {
19+
if is_palindrome(&s[i+1..=j]).is_none() {
20+
return true;
21+
}
22+
if is_palindrome(&s[i..=j-1]).is_none() {
23+
return true;
24+
}
25+
}
26+
None => {
27+
return true;
28+
}
29+
}
30+
31+
false
32+
}
33+
}

0 commit comments

Comments
 (0)