Highest Value Palindrome

Sort by

recency

|

525 Discussions

|

  • + 0 comments
    def highestValuePalindrome(s, n, k):
        s = list(s)  
        left = 0
        right = n - 1
        mismatches = set()
        while left < right:
            if s[left] != s[right]:
                s[left] = s[right] = max(s[left], s[right])
                mismatches.add(left)
                k -= 1
            if k < 0: 
                return '-1'
            left += 1
            right -= 1
            
        left = 0
        right = n - 1
        while left <= right:
            if left == right: 
                if k > 0:
                    s[left] = '9'
            elif s[left] != '9':  
                if left in mismatches: 
                    if k >= 1:
                        s[left] = s[right] = '9'
                        k -= 1
                elif k >= 2:  
                    s[left] = s[right] = '9'
                    k -= 2
            left += 1
            right -= 1
    
        return ''.join(s)
    
  • + 0 comments
        if n == 1 and k > 0:
            return '9'
        count = sum([1 for i in range(n//2) if s[i] != s[-i-1]])
        if k < count:
            return '-1'
        ex = k-count
        s = list(s)
        for i in range(n//2):
            if s[i] == s[-i-1] != '9' and ex > 1:
                s[i] = s[-i-1] = '9'
                ex -=2
            elif s[i] != s[-i-1]:
                if s[i] == '9' or s[-i-1] == '9':
                    s[i] = s[-i-1] = '9'
                else:
                    if ex > 0:
                        s[i] = s[-i - 1] = '9'
                        ex -= 1
                    else:
                        s[i] = s[-i - 1] = max(s[i], s[-i - 1])
        if ex > 0 and n % 2 == 1:
            s[n//2] = '9'
        return ''.join(s)
    
  • + 0 comments

    What about this condition???? 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.

    It seems the example 1 does not meet it.

    Should we take care of zeros at all here?

    I commented my code that process the condition.

  • + 0 comments

    My version

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'highestValuePalindrome' function below.
    #
    # The function is expected to return a STRING.
    # The function accepts following parameters:
    #  1. STRING s
    #  2. INTEGER n
    #  3. INTEGER k
    #
    _marks=[]
    def _create_palindrom(s):
        count=0
        s=list(s)
        for i in range(len(s)//2):
            #check string
            if (s[i]<s[len(s)-i-1]):
                count+=1
                s[i]=s[len(s)-i-1]
                _marks.append(i)
            elif s[i]>s[len(s)-i-1]:
                s[len(s)-i-1]=s[i]
                count+=1
                _marks.append(i)
        return s, count
                
        
    def _maximize_palindrome(s, k, count):
       if k>0 and len(s)==1:
           s[0]='9'
           return
       for i in range(len(s)//2):
           #change the current symbol
           if k>1 and s[i]<'9' and i not in _marks:
               s[i]='9'
               s[len(s)-i-1]='9'
               k-=2
           elif count>0 and s[i]<'9' and i in _marks:    
                s[i]='9'
                s[len(s)-i-1]='9'
                if count>1:
                   count-=2
                else:
                    count-=1
                    k-=1
       #change the middle         
       if k==1 and len(s)%2>0 and count>=0:
             s[len(s)//2]='9'
               
               
    
    def highestValuePalindrome(s, n, k):
        res, count=_create_palindrom(s)
        if count>k:
            return '-1'
        if count<k:
            _maximize_palindrome(res, k-count, count) 
        return ''.join(res)
        
        # Write your code here
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        k = int(first_multiple_input[1])
    
        s = input()
    
        result = highestValuePalindrome(s, n, k)
    
        fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments
    """
    The code fails for long strings probably because the environment times out but I ran locally and it's working as expected
    """
    
    def highestValuePalindrome(s, n, k): 
        # Write your code here
        count = 0
        sarr = [c for c in s]
        for i in range(n//2):
            if sarr[i] == sarr[n-1-i] and sarr[i] != '9' and k>1:            
                    sarr[i] = '9'
                    sarr[n-i-1] = '9'
                    k-=2
            elif sarr[i] == sarr[n-1-i] and k == 1:
                continue
            elif sarr[i] != sarr[n-1-i] and k > 1:
                if sarr[i] == '9':
                    sarr[n-1-i] = '9'
                    k -= 1
                elif sarr[n-1-i] == '9':
                    sarr[i] = '9'
                    k -= 1
                else:
                    sarr[i] = '9'
                    sarr[n-i-1] = '9'
                    k-=2
                
            elif sarr[i] != sarr[n-1-i] and k == 1:
                if sarr[i] > sarr[n-1-i]:
                    sarr[n-1-i] = sarr[i]
                else:
                    sarr[i] = sarr[n-1-i]
                k -= 1
            if k == 0:
                break
        if k == 1:
            if sarr[n//2] != '9':
                sarr[n//2] = '9'
                k -= 1
        print(sarr)
        retval = 0
        for i in range(n//2):
            if sarr[i] != sarr[n-1-i]:
                retval = -1
                print("will return -1")
        if retval <0:
            return str(retval)
        else:
            return ''.join(sarr)