思路:
首先分之前是不是回文串,是的话,找到第一个能更改小的改,都不能就把最后改成b;
不是回文串的话就分情况讨论,
class Solution {
public:
int l;
string breakPalindrome(string palindrome) {
l = palindrome.size();
if(l <= 1){
return "";
}
if(ifHui(palindrome)){
for(int i = 0; i < l; i++){
if(l % 2 == 1 && i == l / 2){
continue;
}
if(palindrome[i] != 'a'){
palindrome[i] = 'a';
return palindrome;
}
if(i == l - 1){
palindrome[i] = 'b';
return palindrome;
}
}
}else{
int s = 0;
int e = l - 1;
while(s < l){
if(palindrome[s] == 'a'){
}else if(palindrome[s] != 'a' && palindrome[e] != 'a'){
if(s != e){
palindrome[s] = 'a';
return palindrome;
}else{
string temp = palindrome;
temp[s] = 'a';
if(!ifHui(temp)){
return temp;
}
}
}else if(palindrome[s] != 'a' && palindrome[e] == 'a'){
string temp = palindrome;
temp[s] = 'a';
if(!ifHui(temp)){
return temp;
}
}
s++;
e--;
}
}
return palindrome;
}
bool ifHui(string s){
int st = 0;
int e = s.size() - 1;
while(e > st){
if(s[e] != s[st]){
return false;
}
st++;
e--;
}
return true;
}
};