Leetcode Problem 680. Valid Palindrome II

680. Valid Palindrome II

Leetcode Solutions

Valid Palindrome with At Most One Deletion

  1. Define a helper function checkPalindrome that takes the string s and two indices i and j and checks if the substring s[i...j] is a palindrome.
  2. Initialize two pointers, left at the start of the string (0) and right at the end of the string (len(s) - 1).
  3. While left is less than right, compare the characters at s[left] and s[right].
    • If they match, increment left and decrement right and continue.
    • If they don't match, check if s[left+1...right] or s[left...right-1] is a palindrome using the helper function.
    • If either is a palindrome, return true; otherwise, return false.
  4. If the loop completes without finding a mismatch, return true as the entire string is a palindrome.
UML Thumbnail

Alternative Approach: Brute Force with Early Termination

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...