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.
stringhighestValuePalindrome(strings,intn,intk){inti=0;std::set<int>visited;while(i<s.size()){if(s[i]!=s[n-i-1]){if(k==0)return"-1";autovalue=std::max(s[i],s[n-i-1]);s[i]=s[n-i-1]=value;k--;visited.insert(i);}i++;}// in here s is a palindromei=0;while(i<s.size()&&k>0){if(s[i]!='9'){if(visited.find(i)!=visited.end()){s[i]=s[n-i-1]='9';k--;}elseif(k>=2){s[i]=s[n-i-1]='9';k-=2;}}i++;}if(n%2==1&&k>0)s[n/2]='9';returns;}
Highest Value Palindrome
You are viewing a single comment's thread. Return to all comments →
Short version, easy to understand