Highest Value Palindrome

  • + 4 comments

    I do not agree. My solution does not contain more than 2 if's. The logic is very simple:

    • go through the half of the string with i idx, and compare s[i] with s[n-i-1]. If they differ, set both to the largest, increment the change count.
    • if the change count is gt than the allowed, quit with "-1"
    • otherwise go through again on the half of the string, and where it is not '9', replace it with '9', while incrementing the change count further until no more changes left. The only complicated part is that some numbers will be changed twice, those should be accounted for in a bit vector in the first loop.
    • if the string is of odd size, and there are any free changes left, replace the middle with '9'.