You are viewing a single comment's thread. Return to all comments →
def highestValuePalindrome(s, n, k): s = list(s) least_changes = 0 for i in range(len(s) // 2): if s[i] != s[len(s) - i - 1]: least_changes += 1 if least_changes > k: return '-1' for i in range(len(s) // 2): if s[i] != s[len(s) - i - 1]: if k > least_changes: if s[i] != '9': s[i] = '9' k -= 1 if s[len(s) - i - 1] != '9': s[len(s) - i - 1] = '9' k -= 1 least_changes -= 1 else: s[i] = max(s[i], s[len(s) - i - 1]) s[len(s) - i - 1] = max(s[i], s[len(s) - i - 1]) least_changes -= 1 k -= 1 elif k - least_changes >= 2 and s[i] != '9': s[i] = '9' s[len(s) - i - 1] = '9' k -= 2 if k > 0 and len(s) % 2 != 0: s[len(s) // 2] = '9' return "".join(s)
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 →