Palindrome Index

  • + 0 comments
    def palindromeIndex(s):
        def is_palindrome(s, left, right):
            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]:
                if is_palindrome(s, left + 1, right):
                    return left
                elif is_palindrome(s, left, right - 1):
                    return right
                else:
                    return -1
            left += 1
            right -= 1
        return -1