We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defhighestValuePalindrome(s,n,k):# Write your code heres=list(s)deduct=[0foriinrange(n)]# 1. make sure symmetric...deduction=[]curr=kmid=n//2foriinrange(mid):ifs[i]!=s[n-1-i]:#needalteringcurr-=1ifcurr<0:return"-1"deduct[i]=1ifs[i]>s[n-1-i]:s[n-1-i]=s[i]elifs[i]<s[n-1-i]:s[i]=s[n-1-i]# print(curr,s)# 2. maxize...foriinrange(mid):ifs[i]!='9'andcurr>=2-deduct[i]:#maxizeandaffords[i]='9's[n-1-i]='9'curr-=(2-deduct[i])# print(curr,s)ifcurr==0:break# 3. special case midifcurr>0andn%2==1:s[mid]='9'return"".join(s)
Cookie support is required to access HackerRank
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 →
symmetry, maxize, mid