You are viewing a single comment's thread. Return to all comments →
C++
int palindromeIndex(string s) { for (size_t i = 0; i < s.length() / 2; i++) { if (s[i] != s[s.length() - 1 - i]) { string tmp1 = s, tmp2 = s; tmp1.erase(i, 1), tmp2.erase(tmp2.length() - 1 - i, 1); reverse(tmp1.begin(), tmp1.begin() + tmp1.length() / 2); reverse(tmp2.begin(), tmp2.begin() + tmp2.length() / 2); if ((s.length() - 1) % 2) { if (tmp1.substr(0, tmp1.length() / 2) == tmp1.substr(tmp1.length() / 2 + 1, tmp1.length() / 2)) return i; else if (tmp2.substr(0, tmp2.length() / 2) == tmp2.substr(tmp2.length() / 2 + 1, tmp2.length() / 2)) return s.length() - 1 - i; else return -1; } else { if (tmp1.substr(0, tmp1.length() / 2) == tmp1.substr(tmp1.length() / 2, tmp1.length() / 2)) return i; else if (tmp2.substr(0, tmp2.length() / 2) == tmp2.substr(tmp2.length() / 2, tmp2.length() / 2)) return s.length() - 1 - i; else return -1; } } } return -1; }
Seems like cookies are disabled on this browser, please enable them to open this website
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
C++