0

I found this question to be interesting and I would like to share this here and find reasonably good codes, specific to py :

Given a string S having characters from English alphabets ['a' - 'z'] and '.' as the special character (without quotes). Write a program to construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case alphabet.

Definition:

The smallest lexicographical order is an order relation where string s is smaller than t, given the first character of s (s1 ) is smaller than the first character of t (t1 ), or in case they are equivalent, the second character, etc.

For example : "aaabbb" is smaller than "aaac" because although the first three characters are equal, the fourth character b is smaller than the fourth character c.

Input Format:

String S

Output Format:

Print lexicographically smallest palindrome after filling each '.' character, if it possible to construct one. Print -1 otherwise.

Example-1

Input: a.ba

Output: abba

Example-2:

Input: a.b

Output: -1

Explanation:

In example 1, you can create a palindrome by filling the '.' character by 'b'.

In example 2, it is not possible to make the string s a palindrome.

2 Answers2

1

You can't just copy paste questions from NPTEL assignments and ask them here without even trying! Anyways,since the "code" is your only concern,try copy pasting the lines below:

    word = input()
    length = len(word)
    def SmallestPalindrome(word, length):
        i = 0
        j = length - 1
        word = list(word)  #creating a list from the input word
        while (i <= j):  
              if (word[i] == word[j] == '.'): 
                  word[i] = word[j] = 'a'  
              elif word[i] != word[j]:  
                  if (word[i] == '.'):
                     word[i] = word[j]
                  elif (word[j] == '.'):
                     word[j] = word[i]
                  else:  # worst case situation when palindrome condition is not met
                     return -1
             i = i + 1  
             j = j - 1 
        return "".join(word)  # to turn the list back to a string
    print(SmallestPalindrome(word, length)) #Print the output of your function
  • 1
    I believe I said this,'I found this question to be interesting and I would like to share this here' before posting the question. Yes, It is from NPTEL, why would I deny it. FYI, I did try my own codes, but I just couldn't get the hang of it, and I tried searching on the Internet to this particular question. I thought I could get better answers from good coders around here. I just wanted to share this and get to know all the possible codes I could have tried it with. I am sorry if I have offended you. – Gautham Vijaikumar Oct 05 '18 at 13:24
-1
s=input()
s=list(s)
n=len(s)
j=n
c=0
for i in range(n):
    j=j-1
    if((s[i]==s[j]) and (i==j) and (s[i]=='.' and s[j]=='.')):
      s[i]='a'
      s[j]='a'
    elif(s[i]==s[j]):
        continue
    elif((s[i]!=s[j]) and (i!=j) and (s[i]=='.' or s[j]=='.')):
        if(s[i]!='.'):
            s[j]=s[i]
        else:
            s[i]=s[j]
    elif((i==j) and (s[i]=='.')):
       s[i]=a


    else:

        c=c+1
        break
if(c<1):
    for k in s:
        print(k,end="")
else:print("-1")