Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0680 - Valid Palindrome II
    0680 - Valid Palindrome II
    0680 - Valid Palindrome II
    0680 - Valid Palindrome II

    0680 - Valid Palindrome II

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/valid-palindrome-ii/

    Solution 1 - Two Pointer with One Skip

    Given a string s, return true if s can be a palindrome after deleting at most one character from it. Use two pointers from both ends — when a mismatch is found, check if skipping either the left or right character yields a valid palindrome.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0680.1 - Solution 1 - Two Pointer with One Skip """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def validPalindrome(self, s: str) -> bool:
            """Valid Palindrome II Function"""
    
            def is_palindrome(left: int, right: int) -> bool:
                """Helper to check if substring s[left:right+1] is a palindrome."""
                while left < right:
                    if s[left] != s[right]:
                        return False
                    left += 1
                    right -= 1
                return True
    
            left, right = 0, len(s) - 1
            while left < right:
                if s[left] != s[right]:
                    # Try skipping left char or right char
                    return is_palindrome(left + 1, right) or is_palindrome(left, right - 1)
                left += 1
                right -= 1
            return True
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().validPalindrome("aba"))       # True
        print(Solution().validPalindrome("abca"))      # True
        print(Solution().validPalindrome("abc"))        # False
        print(Solution().validPalindrome("racecar"))    # True
        print(Solution().validPalindrome("deeee"))      # True
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()