Skip to content

Instantly share code, notes, and snippets.

@hderms
Created February 25, 2021 17:53
Show Gist options
  • Select an option

  • Save hderms/a7da69d79c31e8c856cadd22de8d704d to your computer and use it in GitHub Desktop.

Select an option

Save hderms/a7da69d79c31e8c856cadd22de8d704d to your computer and use it in GitHub Desktop.
determines if a string is a palindrome, if one character is deleted
impl Solution {
pub fn valid_palindrome(s: String) -> bool {
let mut i: usize = 0;
let mut j: usize = s.len() - 1;
let s = s.as_bytes();
loop {
if s[i] != s[j] {
return Solution::is_palindrome(s.clone(), i + 1, j) || Solution::is_palindrome(s.clone(), i, j - 1);
}
i += 1;
j -= 1;
if i > j {
break;
}
}
true
}
fn is_palindrome(s: &[u8], left: usize, right: usize) -> bool {
let mut l = left;
let mut r = right;
while (l <= r) {
if (s[l] != s[r]) {
return false;
}
l += 1;
r -= 1;
}
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment