Created
June 4, 2016 13:15
-
-
Save ilpropheta/4e4ebe0e83d2ae54619b762c7541cd0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const auto diffIt = mismatch(begin(S), begin(S) + S.size()/2, rbegin(S)).first; // first iterator | |
const auto diffIdx = distance(begin(S), diffIt); // 0-based index of the mismatch | |
const auto toCheck = S.substr(0, diffIdx) + S.substr(diffIdx + 1); | |
if (equal(begin(toCheck), begin(toCheck) + toCheck.size()/2, rbegin(toCheck))) // is palindrome? | |
cout << diffIdx << endl; | |
else | |
cout << S.size() - diffIdx - 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment