回文-是否有可能使我的代码更快

I have an ASCII-only string which is either already a palindrome, or else can be made a palindrome by removing one character. I need to determine whether it's already a palindrome, and if not, I need to find the index of the character that would need to be removed. For example, if the string is 'aaba', then it can be made into the palindrome 'aba' by removing the first character, so I need to return 0.

I have working code, but I am wondering if it is possible to make it faster, because I need to work with many long strings.

Here is my code:

package main

import (
    "fmt"
    )

func Palindrome(s string) bool {
    var l int = len(s)

    for i := 0; i < l / 2; i++ {
        if s[i] != s[l - 1 - i] {
            return false;
        }
    }

    return true
}

func RemoveChar(s string, idx int) string {
    return s[0:idx-1] + s[idx:len(s)]
}

func findIdx(s string) int {
    if Palindrome(s) {
        return -1
    }

    for i := 0; i < len(s); i++ {
        if Palindrome(RemoveChar(s, i + 1)) {
            return i
        }
    }

    return -2
}

func main() {
    var s string = "aabaab"
    fmt.Println(findIdx(s))
}

Here is a much more efficient approach:

func findIdx(s string) int {
    for i := 0; i < len(s) / 2; i++ {
        if s[i] != s[len(s) - i - 1] {
             if isPalindrome(s[i+1:len(s)-i]) {
                 return i
             } else {
                 return len(s) - i - 1
             }
        }
    }

    return -1
}

It just proceeds from the ends of the string until it finds a pair of bytes that should match, if the string were a palindrome, but that do not. It then knows that it will return the index of one of these two bytes, so it only needs to perform one "is this a palindrome?" check; and it doesn't need to use the RemoveChar function, since the "is this a palindrome?" check only needs to consider the middle portion of the string (that hasn't already been examined).

This should be very slightly more efficient than ruakh's solution. You shouldn't have to use isPalindrome() to check that s[i + 1:len(s) - i] is a palindrome because it's quicker to check that s[i:len(s) - i - 1] is not a palindrome. In the solution below, in most cases j won't get very far at all before the function returns.

func findIdx(s string) int {
    var n int = len(s) 
    for i := 0; i < n / 2; i++ {
        if s[i] != s[n - i - 1] {
             for j := 0; ;j++ {
                 if s[i + j] != s[n - 2 - i - j] {
                     return i;
                 }
                 if s[i + j + 1] != s[n - 1 - i - j] {
                     return n - 1 - i; 
                 }
             }
        }
    }

    return -1
}