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.
Yes, I thought of something similar and here you have done the same!
Python
defhighestValuePalindrome(s:str,n,k):left=list(s[:n//2])right=list(s[n//2+1:][::-1]) if n % 2 == 1 else list(s[n//2:][::-1])remainingCount=kdifference=0length=len(left)foriinrange(length):if(left[i]!=right[i]):difference+=1foriinrange(length):x=left[i]y=right[i]if(x==y):if(x=='9'):continueelif(remainingCount-2>=(difference)):left[i]='9'right[i]='9'remainingCount-=2elif(x!=y):if(x=='9'):if(remainingCount-1>=(difference-1)):right[i]='9'difference-=1remainingCount-=1else:return-1elif(y=='9'):if(remainingCount-1>=(difference-1)):left[i]='9'difference-=1remainingCount-=1else:return-1else:if(remainingCount-2>=(difference-1)):left[i]='9'right[i]='9'difference-=1remainingCount-=2elif(remainingCount-1>=(difference-1)):if(x>y):right[i]=left[i]else:left[i]=right[i]difference-=1remainingCount-=1else:return-1if(n%2==0):ans="".join(left+(right[::-1]))if(n%2==1):mid=['9']if(remainingCount>0)else[s[n//2]]ans="".join(left+mid+(right[::-1]))return(ans)
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 →
Yes, I thought of something similar and here you have done the same! Python