Skip to main content
Back to problems
#680
Easy Algorithms

Valid palindrome ii

Two Pointers String Greedy
44.0% acceptance
Feb 20, 2026
8958
513
Given a string s, return true if it can be a palindrome after deleting at most one character.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn valid_palindrome(s: String) -> bool {
    let s = s.as_bytes();
    let (mut l, mut r) = (0, s.len() - 1);
    while l < r {
      if s[l] != s[r] {
        return Self::is_palindrome(s, l + 1, r) || Self::is_palindrome(s, l, r - 1);
      }
      l += 1;
      r -= 1;
    }
    true
  }

  fn is_palindrome(s: &[u8], mut l: usize, mut r: usize) -> bool {
    while l < r {
      if s[l] != s[r] { return false; }
      l += 1;
      r -= 1;
    }
    true
  }
}