Palindrome Index | Lei Jiang Coding
You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome.
There will always be a valid solution.
In case the string is already a palindrome, then
http://www.cnblogs.com/lautsie/p/3911501.html
Read full article from Palindrome Index | Lei Jiang Coding
You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome.
There will always be a valid solution.
In case the string is already a palindrome, then
-1 is also a valid answer along with possible indices.http://www.cnblogs.com/lautsie/p/3911501.html
string s; cin >> s; int l = 0; int r = s.size() - 1; while (l < r && s[l] == s[r]) { l++; r--; } if (l >= r) { cout << -1 << endl; continue; } int ll = l + 1; int rr = r; while (ll < rr && s[ll] == s[rr]) { ll++; rr--; } if (ll >= rr) { cout << l << endl; } else { cout << r << endl; }int palindromeIndex( string s ){ int start = 0, end = s.size() - 1; bool flag = false; while (start < end && s[ start ] == s[ end ]){ start++; end--; } if ( start >= end ) return -1;//palindrome if (s [ start + 1 ] != s [ end ]) return end; else if ( s [ start ] != s [ end - 1] ) return start; else{ int begin = start, last = end, left = start + 1, right = end - 1; while(s[ begin ] == s[ right ] && s[ left ] == s[ last ] ){ begin++; last--; left++; right--; } return s[begin] != s[right] ? start : end; } }