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 :) )
std::string highestValuePalindrome( std::string & _str, int const & _strSz, int & _changesAllowed ){ static constexpr char MAX_DIGIT = '9'; static constexpr int SHIFT = 10; for(auto first = std::begin(_str), last = --std::end(_str); first < last; ++first, --last ){ if(*first == *last){ continue; } if(_changesAllowed == 0){ return "-1"; } *first = std::max(*first, *last); *last = *first = *first == MAX_DIGIT ? *first : *first + SHIFT; --_changesAllowed; } for(auto first = std::begin(_str), last = --std::end(_str); first < last; ++first, --last ){ if(_changesAllowed == 0){ for(; first < last; ++first, --last){ if(*first <= MAX_DIGIT){ continue; } *last = *first -= SHIFT;; } return _str; } if(*first > MAX_DIGIT){ *last = *first = MAX_DIGIT; --_changesAllowed; }else{ if(_changesAllowed == 1){ continue; } if(*first != MAX_DIGIT){ *last = *first = MAX_DIGIT; _changesAllowed -= 2; } } } if(_changesAllowed >= 1 && _strSz % 2 == 1){ _str.at(_strSz / 2) = MAX_DIGIT; } return _str; }
Seems like cookies are disabled on this browser, please enable them to open this website
Highest Value Palindrome
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 :) )