Problem:
 Score : 300 points    Problem Statement E869120 found a chest which is likely to contain treasure. However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters. He also found a string S' , which turns out to be the string S with some of its letters (possibly all or none) replaced with ? .  One more thing he found is a sheet of paper with the following facts written on it:   Condition 1: The string S contains a string T as a contiguous substring.  Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.   Print the string S . If such a string does not exist, print UNRESTORABLE .      Constraints  1 \leq |S'|, |T| \leq 50  S' consists of lowercase English letters and ? .  T consists of lowercase English letters.         Input Input is given from Standard Input in the following format:  S  T'       Output Print the string S . If such a string does not exist, print UNRESTORABLE instead.        Sample Input 1 ?tc????
coder      Sample Output 1 atcoder  There are 26 strings that satisfy Condition 1: atcoder , btcoder , ctcoder ,..., ztcoder .
Among them, the lexicographically smallest is atcoder , so we can say S = atcoder .       Sample Input 2 ??p??d??
abc      Sample Output 2 UNRESTORABLE  There is no string that satisfies Condition 1, so the string S does not exist.

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(Sd, T):
    L = len(T)
    for i in range(len(Sd)-L, -1, -1):
        for j in range(L):
            if Sd[i+j] != '?' and Sd[i+j] != T[j]:
                break
        else:
            k = i
            break
    else:
        return 'UNRESTORABLE'

    S = [c for c in Sd]

    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'

    for i in range(L):
        S[k+i] = T[i]
    
    return ''.join(S)

def main():
    Sd = read_str()
    T = read_str()
    print(slv(Sd, T))


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
T = input()


def main():
    if len(S) < len(T):
        print("UNRESTORABLE")
        return

    for i in range(len(S) - len(T), - 1, -1):
        for j in range(len(T)):
            if S[i + j] != "?" and S[i + j] != T[j]:
                break
        else:
            print(S[:i].replace("?", "a") + T +
                  S[i + len(T):].replace("?", "a"))
            break
    else:
        print("UNRESTORABLE")

    return


main()

Solution (Python):
S = str(input())
T = str(input())
numS=len(S)
numT=len(T)
S+=" "
ref=""
cand=""
judge=True
ans=-1
result=""
for i in range(0,numS-numT+1):
    cand=""
    ref=S[i:i+numT]
    judge=True
    
    for j in range(0,numT):
        if(ref[j]!=T[j] and ref[j]!="?"):
            judge=False
    if(judge):
        cand=S[:i]+T+S[i+numT:]
        tmp=""
        for i in range(0,numS):
            if(cand[i]=="?"):
                tmp+="a"
            else:
                tmp+=cand[i]
        if(result==""):
            result=tmp
        else:
            if(result>tmp):
                result=tmp
    
if(result==""):
    print("UNRESTORABLE")
else:
    print(result)

Solution (Python):
import re
s=input().replace('?','.')
t=input()
ans='UNRESTORABLE'
for i in range(len(s)-len(t),-1,-1):
    if re.match(s[i:i+len(t)],t):
        s=s.replace('.','a')
        ans=s[:i]+t+s[i+len(t):]
        break
print(ans)

Solution (Python):
S, T = [str(input()) for _ in range(2)]
ans_list = []
for i in range(len(S)):
    if not i+len(T) <= len(S):
        break
    else:
        tmp = S
        
        flag = True
        for j in range(i, i+len(T)):
            if S[j] == '?':
                
                tmp = tmp[:j] + T[j-i] + tmp[j+1:]
            elif S[j] == T[j-i]:
                continue
            else:
                flag = False
                break
        if flag:
            ans_list.append(tmp.replace('?', 'a'))
if ans_list:
    print(sorted(ans_list)[0])
else:
    print('UNRESTORABLE')

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    S = [str(c) for c in input().strip()]
    T = [str(c) for c in input().strip()]

    n = len(S) - len(T) + 1
    candidate = []
    for i in range(n):
        check = [False] * len(T)
        for j in range(len(T)):
            if T[j] == S[j+i] or S[j+i] == '?':
                check[j] = True

        if all(check):
            can = ''
            B = S[:i] + T + S[i+len(T):]
            for a in B:
                if a == '?':
                    can += 'a'
                else:
                    can += a

            candidate.append(can)

    if len(candidate) == 0:
        print('UNRESTORABLE')
    else:
        candidate = sorted(candidate)
        print(candidate[0])


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
T = input()

def f():
    l = []
    for i in range(len(S)):
        if i + len(T) > len(S):
            break
        flag = True
        for j in range(len(T)):
            if S[i + j] not in [T[j], "?"] :
                flag = False
                break
        if flag:
            s = S
            s = s[:i] + T + s[i+len(T):]
            s = s.replace("?", "a")
            l.append(s)
    l.sort()
    if len(l):
        return l[0]
    else:
        return "UNRESTORABLE"

print(f())

Solution (Python):
S = input()
T = input()
a = []
for i in range(len(S)):
    ok = True
    for j in range(len(T)):
        if i+j >= len(S):
            ok = False
            break
        if S[i+j] == '?':
            continue
        if S[i+j] != T[j]:
            ok = False
            break
    if not ok:
        continue
    U = list(S)
    for j in range(len(T)):
        U[i+j] = T[j]
    for i in range(len(U)):
        if U[i] == '?':
            U[i] = 'a'
    a.append("".join(U))
if a:
    print(sorted(a)[0])
else:
    print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
n = len(s)
m = len(t)
ans = []
for i in range(n - m + 1):
    flag = True
    for j in range(m):
        if s[i+j] != '?' and s[i+j] != t[j]:
            flag = False
            break
    if flag:
        ans.append(s[:i].replace('?', 'a') + t + s[i+m:].replace('?', 'a'))
if ans == []:
    print('UNRESTORABLE')
else:
    ans.sort()
    print(ans[0])

Solution (Python):
S = input()
T = input()
if len(S) < len(T):
    print("UNRESTORABLE")
    exit()
for i in range(len(S) - len(T), -1, -1):
    for s, t in zip(S[i:i+len(T)], T):
        if s != "?" and s != t:
            break
    else:
        S = list(S)
        S[i:i+len(T)] = list(T)
        S = "".join(S).replace("?", "a")
        print(S)
        exit()
print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
s_list = list(s)

for i in range(len(s)-len(t)+1):
    flg = True
    for j in range(len(t)):
        if not (s[-len(t)-i+j] == "?" or s[-len(t)-i+j] == t[j]):
            flg = False
            break
    
    if flg:
        for j in range(len(t)):
            s_list[-len(t)-i+j] = t[j]
        
        s = "".join(s_list)
        s = s.replace("?", "a")

if s.count("?") > 0:
    print("UNRESTORABLE")
else:
    print(s)

Solution (Python):
S = input()
T = input()
flag = False
for i in range(len(S)):
    k = len(S) - i - len(T)
    for j in range(len(T)):
        if k >= 0 and (S[k] == T[j] or S[k] == '?'):
            k += 1
            if j == len(T) - 1:
                flag = True
                st = len(S) - len(T) - i
        else:
            break
    if flag:
        break
ret = ""
if flag:
    for i in range(st):
        if S[i] == '?':
            ret += 'a'
        else:
            ret += S[i]
    ret += T
    for i in range(st + len(T), len(S)):
        if S[i] == '?':
            ret += 'a'
        else:
            ret += S[i]
else:
    ret = "UNRESTORABLE"
print(ret)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    r,g = IR(2)
    print(2*g-r)


def B():
    n = I()
    k = I()
    a = 1
    for i in range(n):
        if a > k:
            a += k
        else:
            a *= 2
    print(a)

def C():
    def check(a,b):
        for i in range(len(a)):
            if a[i] != b[i] and a[i] != "?":
                return False
        return True
    def f(c):
        if c == "?":return "a"
        return c
    s = S()
    t = S()
    n = len(s)
    l = len(t)
    ans = []
    for i in range(n-l+1)[::-1]:
        if check(s[i:i+l],t):
            c = [f(s[j]) for j in range(i)]
            c += t
            for j in range(i+l,n):
                c += f(s[j])
            ans.append(c)
    if not len(ans):
        print("UNRESTORABLE")
    else:
        ans.sort()
        for i in ans[0]:
            print(i,end = "")
        print()

def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    C()

Solution (Python):
def match(s, t):
    if len(s)!=len(t):
        return False
    for i in range(len(s)):
        if s[i] != "?" and s[i] != t[i]:
            return False
    return True


def main():
    s = input()
    t = input()

    replaced = False
    for l in range(len(s)-len(t), -1, -1):
        r = l + len(t)
        if match(s[l:r], t):
            replaced = True
            s = s[:l] + t + s[r:]
            break
    if not replaced:
        print("UNRESTORABLE")
    else:
        print(s.replace("?", "a"))

if __name__ == '__main__':
    main()

Solution (Python):
SS = list(input())
T = list(input())
nt = len(T)
un = 'UNRESTORABLE'
flag = False
for i in reversed(range(len(SS))):
    if i < nt-1:
        break
    flag2 = True
    for j in range(nt):
        if (SS[i-j] == T[nt-1-j] or SS[i-j] == '?') and flag2:
            continue
        else:
            flag2 = False
            break
    if flag2:
        for k in range(nt):
            SS[i+1-nt+k] = T[k]
        flag = True
        break
if flag:
    for s in range(len(SS)):
        if SS[s] == '?':
            SS[s] = 'a'
    print(''.join(SS))
else:
    print(un)

Solution (Python):
import sys
write = sys.stdout.write


Sdash = input()
Slen = len(Sdash)
T = input()





Tlen = len(T)
Pat = [Tlen for i in range(26)]
for i in range(Tlen):
    Pat[ord(T[i]) - 97] = Tlen - (i+1)


flg = 0 
index = Tlen - 1 
pos = Tlen - 1 
kouho = []

while index <= Slen-1:
    if pos == index - Tlen:
        flg = 1
        kouho.append(pos+1) 
        index += 1 
        pos = index
        continue
    if ord(Sdash[pos]) == 63 or Sdash[pos] == T[Tlen - (index - pos) - 1]:
        pos -= 1
        continue
    else:
        move = Pat[ord(Sdash[pos]) - 97]
        if pos + move <= index:
            index += 1
            pos = index
        else:
            index = pos + move
            pos = index
if flg == 1:
    
    for i in range(kouho[-1]):
        if Sdash[i] == '?':
            write("a")
        else:
            write(Sdash[i])
    write(T)
    for i in range(kouho[-1]+Tlen, Slen):
        if Sdash[i] == '?':
            write("a")
        else:
            write(Sdash[i])
    write("\n")
else:
    print("UNRESTORABLE")

Solution (Python):
def main():
    s = input()
    t = input()

    print(solve(s, t))

def solve(s, t):
    xs = []
    n = len(t)
    for i in range(len(s) - n + 1):
        if match(s[i:i+n], t):
            xs.append(s[:i].replace('?', 'a') + t + s[i+n:].replace('?', 'a'))
    
    if xs:
        return min(xs)

    return 'UNRESTORABLE'

def match(x, y):
    for i, j in zip(x, y):
        if i != '?' and i != j:
            return False
    return True

main()

Solution (Python):
s = input()
t = input()

l = []

for i in range(len(s)-len(t) + 1):
    f = True
    for j in range(len(t)):
        if s[i+j] != '?' and s[i+j] != t[j]:
            f = False
            break
    if f:
        a = s[:i] + t + s[i+len(t):]
        l.append(a.replace('?','a'))
if len(l)>0:
    print(min(l))
else:
    print('UNRESTORABLE')

Solution (Python):
def main():
    S = input()
    T = input()
    for i in range(len(S) - len(T), -1, -1):
        ok = True
        for j in range(len(T)):
            if S[i+j] != "?" and S[i+j] != T[j]:
                ok = False
                break
        if ok:
            ans = (S[:i] + T + S[i+len(T):]).replace("?", "a")
            print(ans)
            return
    print("UNRESTORABLE")


if __name__ == "__main__":
    main()

Solution (Python):
S = input()
T = input()
X = "UNRESTORABLE"
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            pass
        else:
            break
    else:
        res.append(S[:inds]+T + S[inds + len(T):len(S)])

if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

Solution (Python):
S = input()
T = input()
X = "UNRESTORABLE"
ans = ""
x = 0
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            pass
        else:
            break
    else:
        res.append(ans+T + S[inds + len(T):len(S)])
    ans += s

if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

Solution (Python):
S = input()
T = input()
ans = "UNRESTORABLE"
for i in range(len(S)-len(T)+1):
  for j in range(len(T)):
    if(T[j] == S[i+j] or S[i+j] == "?"):
      continue
    else:
      break
  else:
    ans = S[:i]+T+S[i+len(T):]
    ans = ans.replace("?", "a")
print(ans)

Solution (Python):
s=input()
t=input()
ls=len(s)
lt=len(t)

y=0
for i in list(reversed(range(ls-lt+1))):
    L=[]
    for j in range(lt):
        L.append((s[i+j]==t[j] or s[i+j]=='?'))
    if all(L):
        s=list(s)
        s[i:i+lt]=list(t)
        s="".join(s)
        s=s.replace('?','a')
        print(s)
        y=1
        break
if y==0:
    print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()
X = "UNRESTORABLE"
ans = ""
x = 0
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    flag = 0
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            flag += 1
    if flag == len(T):
        res.append(ans+T + S[inds + len(T):len(S)])
    ans += s
if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

Solution (Python):
S = input()
T = input()
ans = []
for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j] != T[j] and S[i+j] != '?':
            break
    else:
        tmp = S[:i] + T + S[i+len(T):]
        ans.append(tmp.replace('?', 'a'))
if ans:
    print(ans[-1])
else:
    print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()

ls = len(s)
lt = len(t)
ANS = []

for i in range(ls) :
    if s[i] == t[0] or s[i] == "?" :
        co = 0
        for k in range(lt) :
            if 0 <= i+k < ls and (s[i+k] == t[k] or s[i+k] == "?") :
                co += 1
        if co == lt :
            ANS.append((s[:i]+t+s[i+lt:]).replace("?","a"))
            

ANS.sort()
if len(ANS) > 0 :
    print(ANS[0])
else :
    print("UNRESTORABLE")

Solution (Python):
SS=input()
T=input()
can=False
for i in reversed(range(len(SS)-len(T)+1)):
    can1=True
    for j in range(len(T)):
        if SS[i+j]==T[j] or SS[i+j]=="?":
            continue
        can1=False
        break
    if can1:
        can=True
        break
if can:
    S=""
    for j in range(len(SS)):
        if i<=j<i+len(T):
            S+=T[j-i]
        elif SS[j]=="?":
            S+="a"
        else:
            S+=SS[j]
    print(S)
else:
    print("UNRESTORABLE")

Solution (Python):
s=input()
t=input()
n=len(s)
m=len(t)
ans=-1
for i in range(n-m+1):
    sum=0
    for j in range(m):
        if s[i+j]==t[j] or s[i+j]=='?':
            sum+=1
    if sum==m:
        ans=i
if ans==-1:
    print('UNRESTORABLE')
else:
    s=s[:ans]+t+s[ans+m:]
    b=s.replace('?','a')
    print(b)

Solution (Python):
def match(s, t, i):
    for pos in range(len(t)):
        if s[i + pos] != "?" and t[pos] != s[i + pos]:
            return False
    return True


def main():
    s = input()
    t = input()

    for i in reversed(range(len(s) - len(t) + 1)):
        if match(s, t, i):
            s = s[:i] + t + s[i + len(t):]
            print(s.replace("?", "a"))
            return
    print("UNRESTORABLE")


if __name__ == '__main__':
    main()

Solution (Python):

S=input()
T=input()
for s in range(len(S)-len(T)+1)[::-1]:
	for t in range(len(T)):
		if S[s+t]!='?':
			if S[s+t]!=T[t]:
				break
	else:
		ans=''
		for ss in S[:s]:
			if ss=='?':
				ans+='a'
			else:
				ans+=ss
		ans+=T
		for ss in S[s+len(T):]:
			if ss=='?':
				ans+='a'
			else:
				ans+=ss
		print(ans)
		exit()
print('UNRESTORABLE')

Solution (Python):
sd = input()
t = input()
n = len(sd)
m = len(t)
s = []
for i in range(n - m, -1, -1):
    t_kamo = sd[i:i + m]
    for j in range(m + 1):
        if j == m:
            print((sd[:i] + t + sd[i + len(t):]).replace("?", "a"))
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:
            break
print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()

ls = len(s)
lt = len(t)
Ans = []
for i in range(ls - lt + 1):
    ss = s[i:i+lt]
    lss = len(ss)
    cnt = 0
    for j in range(lss):
        if s[i + j] == '?' or t[j] == ss[j]:
            cnt += 1
        else:
            break
    else:
        if cnt == lss:
            ans = ''
            for j in range(i):
                if s[j] == '?':
                    ans += 'a'
                else:
                    ans += s[j]
            ans += t
            for j in range(i + lt, ls):
                if s[j] == '?':
                   ans += 'a'
                else:
                    ans += s[j]
            else:
                Ans.append(ans)
if len(Ans) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(Ans)[0])

Solution (Python):
S = input()
T = input()
n = len(S)
m = len(T)

flag = 0
ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if S[i + j] != '?' and S[i + j] != T[j]:
            break
    else:
        S2 = S[: i] + T + S[i + m :]
        ans = S2.replace('?', 'a')
        
print(ans)

Solution (Python):
S = input()
T = input()
n = len(S)
m = len(T)

flag = 0
ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if S[i + j] != '?' and S[i + j] != T[j]:
            break
    else:
        S2 = S[: i] + T + S[i + m :]
        flag = 1
if flag:
    ans = S2.replace('?', 'a')
        
print(ans)

Solution (Python):
S = input()
T = input()


tmp = -1
for i in range(len(S) - len(T) + 1):
    start = -1
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            start = i
        else:
            start = -1
            break
                       
    if start != -1:
                tmp = i
if tmp == -1:
    print('UNRESTORABLE')
    
else:
    res = ''
    count = 0
    for i in range(len(S)):
        if tmp <= i < tmp + len(T):
            res += T[count]
            count += 1
            
        elif S[i] == '?':
            res += 'a'
            
        else:
            res += S[i]

    print(res)

Solution (Python):
S = input()
T = input()


    
if True:
    tmp = -1
    for i in range(len(S) - len(T) + 1):
        start = -1
        for j in range(len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                start = i
            else:
                start = -1
                break
                
        
        if start != -1:
                tmp = i
    if tmp == -1:
        print('UNRESTORABLE')
    else:
        res = ''
        count = 0
        for i in range(len(S)):
            if tmp <= i < tmp + len(T):
                res += T[count]
                count += 1
            elif S[i] == '?':
                res += 'a'
            else:
                res += S[i]

        print(res)

Solution (Python):
S = list(input())
T = list(input())

flag = False
for i in range(len(S)-len(T),-1,-1):
    count = 0
    for j in range(len(T)):
        
        if T[j] == S[i+j] or S[i+j] == '?':
            count += 1
        else:
            break
    
    if count == len(T):
        flag = True
        for j in range(len(T)):
            S[i+j] = T[j]
        break

if not flag:
    print('UNRESTORABLE')
    exit()

for i in range(len(S)):
    if S[i] == '?':
        S[i] = 'a'

print("".join(S))

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
s=S()
t=S()
rest=False
ii=0
for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?":
            break
    else:
        rest=True
        ii=i
if rest:
    for j in range(len(t)):
        s[ii+j]=t[j]
    for i in range(len(s)):
        if s[i]=="?":
            s[i]="a"
    for i in range(len(s)):
        print(s[i],end="")
else:
    print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()

ind = 0
def search(ind, target, source, target_pos):
    if target_pos + ind >= len(target) and ind < len(source):
        return ""
    if ind == len(source):
        if target_pos + len(source) <= len(target):
            return target[:target_pos] + source + target[target_pos+len(source):].replace("?","a")
        else:
            return target[:target_pos] + source
    if target[target_pos + ind] == source[ind] or target[target_pos + ind] == "?":
        result = search(ind+1, target, source, target_pos)
    else:
        target = target[:target_pos].replace("?","a")+target[target_pos:]
        result = search(0, target, source, target_pos+1)
    return result

best = ""
for i in range(len(T)):
    result = search(0, S[::-1], T[::-1], i)[::-1].replace("?","a")
    if result != "" and (best == "" or best > result):
        best = result
if best == "":
    print("UNRESTORABLE")
else:
    print(best)

Solution (Python):
S=input()
T=input()
s=0
flag1=False
for i in range(len(S)-len(T)+1):
    flag2=True
    for j in range(len(T)):
        if S[i+j]!=T[j] and S[i+j]!='?':
            flag2=False
    if flag2==True:
        flag1=True
        s=i
if flag1==False:
    print('UNRESTORABLE')
else:
    for i in range(len(S)):
        if i>=s and i-s<len(T):
            print(T[i-s],end='')
        elif S[i]=='?':
            print('a',end='')
        else:
            print(S[i],end='')

Solution (Python):

S = input()
T = input()
N = len(S)

def restore(s):
    t = list(s[:])
    for i in range(len(t)):
        if t[i] == "?":
            t[i] = "a"
    return "".join(t)


cand = []
for i in range(N):

    j = i
    ti = 0

    
    while ti < len(T) and j < len(S) and (S[j] == "?" or S[j] == T[ti]):
        ti += 1; j += 1
        

    if ti == len(T):
        cand.append( S[0:i] + T + S[i +  len(T):]  )
        
    else:
        continue

if len(cand) == 0:
    print("UNRESTORABLE")
else:
    ans = restore(cand[-1])
    print(ans)

Solution (Python):



















from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

s = input()
t = input()

if len(s) >= len(t):
    ans_l = []
    for i in range(0, len(s)-len(t)+1):
        s_s = s[i:len(s)-1+i]
        flag = True
        for ssi, ti in zip(s_s, t):
            if ssi == "?":
                continue
            if ssi != ti:
                flag = False
                break
        if flag:
            ans_l.append(i)

    d = []
    for ans_i in ans_l:
        tmp_s = (s[:ans_i]+t+s[len(t)+ans_i:]).replace("?", "a")
        d.append(tmp_s)
    d = sorted(d)
    if len(d) > 0:
        print(d[0])
    else:
        print("UNRESTORABLE")
else:
    print("UNRESTORABLE")

Solution (Python):



def main():
    s_dash = input()
    t = input()
    s_dash_count = len(s_dash)
    t_count = len(t)
    ans = list()

    if s_dash_count >= t_count:
        for k in range(s_dash_count - t_count + 1):
            mod_s_dash = s_dash[k:k + t_count]
            count = 0

            for i in range(t_count):
                if mod_s_dash[i] == t[i]:
                    count += 1
                elif mod_s_dash[i] == '?':
                    count += 1

            if count == t_count:
                s = s_dash[:k] + t + s_dash[k + t_count:]
                s = s.replace('?', 'a')
                ans.append(s)

        if len(ans) == 0:
            print('UNRESTORABLE')
        else:
            print(sorted(ans)[0])

    else:
        print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    s_dash = input()
    t = input()
    s_dash_count = len(s_dash)
    t_count = len(t)
    ans = list()

    if s_dash_count >= t_count:
        for k in range(s_dash_count - t_count + 1):
            mod_s_dash = s_dash[k:k + t_count]
            count = 0

            for i in range(t_count):
                if mod_s_dash[i] == t[i]:
                    count += 1
                elif mod_s_dash[i] == '?':
                    count += 1

            if count == t_count:
                s = s_dash[:k] + t + s_dash[k + t_count:]
                s = s.replace('?', 'a')
                ans.append(s)

        if len(ans) == 0:
            print('UNRESTORABLE')
        else:
            print(sorted(ans)[0])

    else:
        print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
import numpy as np
import sys

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
t = S()

f = 0
for i in range(len(s)-len(t),-1,-1):
    for j in range(len(t)):
        if s[i+j] == t[j] or s[i+j] == "?":
            continue
        else:
            break
    else:
        a = s[:i]+t+s[i+len(t):]
        print(a.replace('?', 'a'))
        break
else:
    print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
res = []

for s_index in range(len(s)-len(t)+1):
    if s[s_index] in ['?', t[0]]:
        s_p = s[s_index:s_index+len(t)]
        if all(s_p[x] in ['?', t[x]] for x in range(len(s_p))):
            res.append(s[:s_index].replace('?', 'a') + t + s[s_index+len(t):].replace('?', 'a'))
if res:
    print(sorted(res)[0])
else:
    print('UNRESTORABLE')

Solution (Python):
s = input()
t = input()

res = []

for s_index in range(len(s)-len(t)+1):
    if s[s_index] in ['?', t[0]]:
        s_p = s[s_index:s_index+len(t)]
        if len([True for x in range(len(s_p)) if s_p[x] in ['?', t[x]]]) == len(t):
            res.append(s[:s_index].replace('?', 'a') + t + s[s_index+len(t):].replace('?', 'a'))

if res:
    print(sorted(res)[0])
else:
    print('UNRESTORABLE')

Solution (Python):

s=input()
t=input()
can=[]
for i in range(1+len(s)-len(t)):
    check=0
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!='?':
            check=1
            break
    if check==0:can.append(i)
if len(can)==0:print('UNRESTORABLE')
else:
    ans=can[-1]
    res=s[:ans]+t+s[ans+len(t):]
    print(res.replace('?','a'))

Solution (Python):
sd = input()
t = input()
sdl = len(sd)
tl = len(t)
for i in range(tl, sdl+1)[::-1]:
    f = False
    for j in range(tl):
        if sd[i-tl+j] != t[j] and sd[i-tl+j] != "?":
            f = True
    if not f:
        res = sd[:i-tl] + t + sd[i-tl+j+1:]
        res = res.replace("?", "a")
        print(res)
        exit()
print("UNRESTORABLE")

Solution (Python):
def match(a, b):
    for i, j in zip(a, b):
        if i != '?' and i != j:
            return False
    return True

def solve(r, t):
    l = len(t)
    m = len(r)
    for i in range(m - l, -1, -1):
        if match(r[i:i+l], t):
            ans = ''
            for j in range(i):
                if r[j] == '?':
                    ans = ans + 'a'
                else:
                    ans = ans + r[j]
            for j in range(l):
                ans = ans + t[j]
            for j in range(i + l, m):
                if r[j] == '?':
                    ans = ans + 'a'
                else:
                    ans = ans + r[j]
            return ans
    return 'UNRESTORABLE'

R = input()
T = input()
print(solve(R, T))

Solution (Python):
ss = input()
t = input()
e = len(ss)
f = len(t)
d = []
try:
    for i in range(e-f+1):
        b = 1
        for j in range(f):
            if ss[i+j] != t[j] and ss[i+j] != '?':
                b = 0
        if b:
            s = ss[:i]+t+ss[i+f:]
            d.append(s.replace('?', 'a'))
    d.sort()
    print(d[0])

except:
    print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
n = 0
ans = ''
flag = False
cnt = 0
for i in range(len(S)-len(T)+1):
  for j in range(len(T)):
    if S[len(S)-i-j-1] == T[len(T)-j-1] or S[len(S)-i-j-1] == '?':
      cnt += 1
  if cnt == len(T):
    n = i
    flag = True
    break
  else:
    cnt = 0
if not flag:
  print('UNRESTORABLE')
else:
  S = S.replace('?','a')
  ans = S[0:len(S)-len(T)-i]+T+S[len(S)-i:len(S)]
print(ans)

Solution (Python):
S = input()
T = input()
ls = len(S)
lt = len(T)
cand = []
for i in range(ls-lt+1):
    frac =  S[i:i+lt]
    if all(map(lambda x:x[0]==x[1] or x[0]=='?',zip(frac, T))):
        cand.append(S[0:i]+T+S[i+lt:ls])
if cand == []:
    print("UNRESTORABLE")
else:
    cand = list(map(lambda x:x.replace('?','a'), cand))
    print(sorted(cand)[0])

Solution (Python):
S = input()
T = input()
ls = len(S)
lt = len(T)
cand = []
for i in range(ls-lt+1):
    frac =  S[i:i+lt]
    for j in range(lt):
        if frac[j] != T[j] and frac[j] != '?': break
    else:
        cand.append(S[0:i] + T + S[i+lt:ls])
if cand == []:
    print("UNRESTORABLE")
else:
    cand = list(map(lambda x:x.replace('?','a'), cand))
    cand.sort()
    print(cand[0])