Palindromes are strings that read the same from the left or right, for example madam or 0110.
You will be given a string representation of a number and a maximum number of changes you can make. Alter the string, one digit at a time, to create the string representation of the largest number possible given the limit to the number of changes. The length of the string may not be altered, so you must consider 's left of all higher digits in your tests. For example is valid, is not.
Given a string representing the starting number, and a maximum number of changes allowed, create the largest palindromic string of digits possible or the string '-1' if it is not possible to create a palindrome under the contstraints.
Make replacements to get .
Make replacement to get .
Complete the highestValuePalindrome function in the editor below.
highestValuePalindrome has the following parameter(s):
string s: a string representation of an integer
int n: the length of the integer string
int k: the maximum number of changes allowed
string: a string representation of the highest value achievable or -1
The first line contains two space-separated integers, and , the number of digits in the number and the maximum number of changes allowed.
The second line contains an -digit string of numbers.
Each character in the number is an integer where .
Sample Input 0
4 1 n = 4, k = 1
3943 s = '3943'
Sample Output 0
Sample Input 1
Sample Output 1
Sample Input 2
Sample Output 2
There are two ways to make a palindrome by changing no more than digits: