Optimal solution in O(n) time using O(1) memory with two pointers, quite straightforward method. Not used that we could have only 'a','b','c' in the string.
int minString(string s){
int i=0,j=s.length()-1;
for(;i<j && s[i]==s[j];){
char d=s[i];
while(i<=j && s[i]==d) i++;
while(i<=j && s[j]==d) j--;
}
return j-i+1;
}
int main(void){
cout<<minString("aabcccabba")<<endl;
cout<<minString("aacbcca")<<endl;
return 0;
}
/* output:
4
1
*/