Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilpropheta/4e4ebe0e83d2ae54619b762c7541cd0d to your computer and use it in GitHub Desktop.
Save ilpropheta/4e4ebe0e83d2ae54619b762c7541cd0d to your computer and use it in GitHub Desktop.
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