Search code examples
pythonstringstring-matching

Lexicographical value


i have a string. I need to replace the character '?' and find the missing character to make it lexicographical string. for Example- if I have string "ab". Its lexicographical value is "aa".Since the first character 'a' is different than second character 'b'. It gives output -1. If I give string "ta?a". In this string If I replace '?' with t. It become lexicographical. It gives output "tata". I am giving input s and k. K is half of length of s. Please help me in this code.

s=input()
k=input()
for i in range(k):
    if (s[i]>=97) and (s[i]<=123):
        if (s[i]==s[i+k]):
            continue
        else:
             s=-1
    
    else:   
        if(s[i]>=s[i+k]):
            s[i+k]=s[i]
        else:
            s[i]=s[i+k]

return s

Solution

  • I'm not entirely sure I understand your question, but here's my attempt:

    s = list(input())
    k = int(input())
    for i in range(k):
        s[i] = s[i+k] if s[i] == '?' else s[i]
        s[i+k] = s[i] if s[i+k] == '?' else s[i+k]
    
    print(''.join(s) if s[:k] == s[k:] else -1)
    

    For inputs ta?a and 2 it will output tata, for ab and 1 it will output -1. Note that the second argument is redundant anyway since you said it's just half the length.