↓Skip to main content
  1. LeetCode/

LeetCode 680: Valid Palindrome II

·2 mins· ·
LeetCode Easy Two-Pointers String Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-30

  • Submissions: Wrong Answer ×7 → Runtime Error ×2 → Time Limit Exceeded → Runtime Error ×3 → Wrong Answer → Runtime Error → Wrong Answer ×3 → Runtime Error → Wrong Answer → Accepted ×2

Approach
#

Compare with two pointers moving inward (Two Pointers Template 1). At the first mismatch s[l] != s[r], only one deletion is allowed, so there are two options: delete s[l] (check s[l+1..r]) or delete s[r] (check s[l..r-1]); either being a palindrome suffices. The part already compared is symmetric.

My Attempts
#

This took 22 submissions. The early idea was “count mismatched pairs and fail beyond one”, but deleting a character shifts all later pairings, so counting is wrong: "bca" has a single mismatched pair (b and a), yet neither deletion yields a palindrome, so the answer is False; those versions returned True. Attempt 1 also compared indices instead of characters.

Solution
#

class Solution:
    def validPalindrome(self, s: str) -> bool:
        def pal(l, r):
            while l < r:
                if s[l] != s[r]:
                    return False
                l, r = l + 1, r - 1
            return True
        l, r = 0, len(s) - 1
        while l < r:
            if s[l] != s[r]:
                return pal(l + 1, r) or pal(l, r - 1)
            l, r = l + 1, r - 1
        return True

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Counting mismatched pairs is not enough: deleting a character shifts every later pairing.

Interview Explanation
#

Two pointers compare inward; at the first mismatch only deleting the left or the right remains, so I check whether either remaining range is a palindrome. Time O(n), space O(1).

Related

LeetCode 696: Count Binary Substrings
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 696 note: split the string into runs of equal characters; each adjacent pair of runs contributes min(previous, current) substrings
LeetCode 541: Reverse String II
·1 min
LeetCode Easy Two-Pointers String
LeetCode 541 note: in every block of 2k reverse the first k; when fewer than k remain reverse them all, which slicing handles automatically
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once
LeetCode 344: Reverse String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 344 note: swap with two pointers and move inward to reverse in place
LeetCode 392: Is Subsequence
·2 mins
LeetCode Easy Two-Pointers String Dynamic-Programming
LeetCode 392 note: two pointers; the pointer on t always advances, and the one on s advances on a match; bound-check both
LeetCode 409: Longest Palindrome
·2 mins
LeetCode Easy Hash-Table String Greedy
LeetCode 409 note: use an even number of each character on the two sides, plus one in the middle if any count is odd