You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star:) )
int palindromeIndex(std::string _s){ int idx = -1; size_t changes = 0; bool isSecondTry = false; auto itRollBakc = _s.begin(); auto chrRollBack = *itRollBakc; for(auto bIt = _s.begin(), eIt = --_s.end(); bIt < eIt; ++bIt, --eIt){ if(*bIt == *eIt){ continue; } if(++changes == 2){ if(isSecondTry){ return -1; } isSecondTry = !isSecondTry; changes = 0; idx = -1; _s.insert(itRollBakc, chrRollBack); bIt = --_s.begin(); eIt = _s.end(); continue; } if(*(bIt + 1) == *eIt && !isSecondTry){ idx = std::distance(_s.begin(), bIt); chrRollBack = *bIt; bIt = itRollBakc = _s.erase(bIt); eIt = _s.end() - idx - 1; }else if(*bIt == *(eIt - 1)){ idx = std::distance(_s.begin(), eIt); eIt = _s.erase(eIt); bIt = _s.begin() + idx; } } return idx; }
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++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star:) )