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.
def highestValuePalindrome(s, n, k):
s=list(s)
i=0
j=len(s)-1
visit=[0]*n
#print(a)
while(i<=j):
if s[i]!=s[j]:
if s[i]
visit[j]+=1
k-=1
i+=1
j-=1
print(s)
if k<0:
return "-1"
i=0
j=n-1
while i<=j:
if(i==j and k>=1):
s[i]='9'
if s[i]<'9':
if (visit[i]==0 and visit[j]==0) and k>=2:
s[i]=s[j]='9'
k-=2
if (visit[i]==1 or visit[j]==1) and k>=1:
s[i]=s[j]='9'
k-=1
i+=1
j-=1
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 →
def highestValuePalindrome(s, n, k): s=list(s) i=0 j=len(s)-1 visit=[0]*n #print(a) while(i<=j): if s[i]!=s[j]: if s[i] visit[j]+=1 k-=1 i+=1 j-=1 print(s)
if k<0: return "-1" i=0 j=n-1 while i<=j: if(i==j and k>=1): s[i]='9' if s[i]<'9': if (visit[i]==0 and visit[j]==0) and k>=2: s[i]=s[j]='9' k-=2 if (visit[i]==1 or visit[j]==1) and k>=1: s[i]=s[j]='9' k-=1 i+=1 j-=1 return "".join(s)