解法:双指针

设置i、j分别指向字符串str两头,同时向中间走

如果str[i]!=str[j],则判断是左边该删除,还是右边该删除

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s;
    int n;
    cin>>n;
    while(n--){
        cin>>s;
        int len=s.size();
        int i=0, j=len-1;
        while(i<=j){
            if(s[i]!=s[j]){
                if(s[i+1]==s[j])
                    cout<<i<<endl;
                else
                    cout<<j<<endl;
                break;
            }
            i++;
            j--;
        }
        if(i>=j)
            cout<<-1<<endl;
    }
    return 0;
}