Palindrome Index

  • + 1 comment
    def isPalindrome2(s):
        return s == s[::-1]
    
    def palindromeIndex2(s):
        if isPalindrome2(s):
            return -1
        else:
            for i in range(len(s) // 2 + 1):
                if s[i] != s[-1 - i]:
                    new_s1: str = s[0:i] + s[i + 1:]
                    new_s2: str = s[0:-1 - i] + s[-i:]
    
                    if isPalindrome2(new_s1):
                        return i
                    elif isPalindrome2(new_s2):
                        return len(s)-i-1
                    else:
                        return -1