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):
s=input()
t=input()
a=-1
n=len(s)
k=len(t)
if k>n:
  print('UNRESTORABLE')
else:
  ans=str()
  for i in range(n):
    ok=True
    if n-i>=k:
      for j in range(k):
        if t[j]!=s[i+j] and s[i+j]!='?':
          ok=False
      if ok:
        a=i
  if a==-1:
    print('UNRESTORABLE')
  else:
    for i in range(n):
      if a<=i<=a+k-1:
        ans+=t[i-a]
      else:
        if s[i]=='?':
          ans+='a'
        else:
          ans+=s[i]
    print(ans)

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

moji = []

for i in range(len(S)):
    j = 0
    cnt = 0
    
    while j < len(T) and i+len(T)-1 < len(S):
        if S[i+j] == T[j] or S[i+j] == '?':
            cnt += 1

        else:
            break
        j += 1
    
    if cnt == len(T):
        moji0 = ''
        if i == 0: moji0 = T+S[i+len(T):len(S)]
        elif i+len(T) > len(S)-1: 
            moji0 = S[0:i]+T
        else:
            moji0 = S[0:i]+T+S[i+len(T):len(S)]

        
        moji1 = moji0.replace('?', 'a')
        moji.append(moji1)

print(min(moji) if moji != [] else "UNRESTORABLE")

Solution (Python):

 
import math
import itertools
import sys
import copy
 





S = str(input())
T = str(input())

SS = []
for i in range(len(S)) :
  if i + len(T) <= len(S) :
    cnt = 0
    st = S[0:i]
    for j in range(len(T)) :
      if S[i+j] == T[j] or S[i+j] == "?" :
        cnt += 1
        st += T[j]
    if cnt == len(T) :
      SS.append((st+S[i+len(T):]).replace("?", "a"))
      

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

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

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

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()

ns = len(s)
nt = len(t)

for i in range(ns-nt, -1, -1):
   x = s[i:i+nt]
   for j in range(nt+1):
      if (j == nt):
         ss = s[:i] + t + s[i+nt:]
         print(ss.replace('?', 'a'))
         exit()

      if (x[j] == '?'):
         continue
      elif (x[j] != t[j]):
         break

print('UNRESTORABLE')

Solution (Python):
Sp = input()
T = input()
n = len(Sp)+1-len(T)
flag = 1
suc = 0
S = ['']*len(Sp)

for i in range(n):
    flag = 1
    for j in range(len(T)):
        if T[j] != Sp[i+j] and Sp[i+j] != '?':
            flag = 0
    
    if flag == 1:
        for l in range(len(Sp)):
            S[l] = Sp[l]
        for k in range(len(T)):
            S[i+k] = T[k]
        for l in range(len(Sp)):
            if S[l] == '?':
                S[l] = 'a'
        suc = 1

if suc == 1:
    print("".join(S))
else:
    print("UNRESTORABLE")

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

keys_list = []
for i in range(len(S) - len(T) + 1):
    flag = True
    key = ""
    for j, char in enumerate(T):
        if S[i+j] != "?" and S[i+j] != T[j]:
            flag = False
            break
    if flag:
        keys_list.append(S[:i] + T + S[i+len(T):])

if len(keys_list) == 0:
    print("UNRESTORABLE")
else:
    ans_list = []
    for key in keys_list:
        ans = key.replace("?", "a")
        ans_list.append(ans)
    print(sorted(ans_list)[0])

Solution (Python):
S_prime = input()
T = input()
lenS = len(S_prime)
lenT = len(T)
existflag = 0
kouho = []
index = 0
for i in range(lenS-lenT+1):
    index = i
    
    for j in range(lenT):
        flag = 1
        if S_prime[i + j] != "?" and S_prime[i + j] != T[j]:
            flag = 0
            break
    if flag == 1:
        kouho.append(
            (S_prime[:index] + T + S_prime[index+lenT:]).replace("?", "a"))



if len(kouho) == 0:
    print("UNRESTORABLE")
else:
    print(min(kouho))

Solution (Python):

s = input()
sn = len(s)
t = input()
tn = len(t)

ss = ""
for i in range(sn-tn+1):
    match = True
    for j in range(tn):
        if s[i+j]=="?" or s[i+j]==t[j]:
            pass
        else:
            match = False
            break
    if match:
        ss = s[:i] + t + s[i+tn:]

if ss=="":
    print("UNRESTORABLE")
    exit()

print(ss.replace("?","a"))

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

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

Solution (Python):
def find(S, T):
    return all(s==t for s, t in zip(S, T) if s!="?")
S, T = input(), input()
L = []
for i in range(len(S)-len(T)+1):
    if find(S[i:i+len(T)],T):
        L.append((S[:i]+T+S[i+len(T):]).replace("?", "a"))
print(min(L) if len(L) else "UNRESTORABLE")

Solution (Python):
S = input()
T = input()
ss = list()
for n in range(len(S)-len(T)+1):
    for m in range(len(T)):
        if S[n+m] not in ["?", T[m]]:
            break
    else:
        ss.append((S[:n]+T+S[n+len(T):]).replace("?","a"))
if len(ss) == 0:
    print("UNRESTORABLE")
else:
    print(min(ss))

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def isEqual(s, t):
    if s == t or t == '?':
        return True
    return False

def main():
    ans = []
    s = input().strip()
    t = input().strip()
    for i in range(len(s) - len(t)+1):
        k = i
        w = i
        flag = True
        for j in range(len(t)):
            if not isEqual(t[j], s[k]):
                flag = False
                break
            k += 1
        if flag:
            q = list(s)
            q[w:w+(len(t))] = t
            q = ''.join(q)
            q = q.replace('?', 'a')
            ans.append(q)
    if len(ans) == 0:
        print('UNRESTORABLE')
    else:
        print(sorted(ans)[0])

if __name__ == '__main__':
    main()

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

s.reverse()
t.reverse()

snum=len(s)
tnum=len(t)
chk=False
for i in range(snum-tnum+1):
    
    cnt=0
    for j in range(i,i+tnum):
        
        if s[j]==t[j-i]:
            cnt+=1
        elif s[j] =="?":
            cnt+=1
        
    if cnt==tnum and chk==False:
        s[i:i+tnum]=t
        chk=True
    else:
        if s[i]=="?":
            s[i]="a"

for i in range(snum):
    if s[i]=="?":
        s[i]="a"

if chk==True:
    print("".join(reversed(s)))
else:
    print("UNRESTORABLE")

Solution (Python):
a = input()
b = input()
ans = ''
f = 1
a = a[::-1]
b = b[::-1]
for ii in range(len(a)-len(b)+1):
    s =0
    for i,j in zip(a[ii:ii+len(b)+1],b):
        if i==j or i =='?':s+=1
    if s == len(b):
        rec = ii
        f = 2
        break
if f != 2:
    print('UNRESTORABLE')
else:
    for i in range(len(a)):
        if i == rec:ans+=b
        elif i < rec+len(b):continue
        elif a[i] == '?':ans+='a'
        else:ans +=a[i] 
    t=(a[:rec]+b+a[len(b)+rec:]).replace('?','a')
    
    print(t[::-1])

Solution (Python):
def check(p, s):
    flg = True
    for i in range(len(tmp)):
        if s[i]=='?' or p[i]=='?':
            continue
        elif s[i] == p[i]:
            continue
        elif s[i] != p[i]:
            flg = False
    return flg


S = input()
T = input()
lst = []

for i in range(len(S)-len(T)+1):
    tmp = S[:i]+T+S[i+len(T):]
    if check(tmp, S):
        lst.append(tmp)

if len(lst)==0:
    ans = 'UNRESTORABLE'
else:
    ans_lst = []
    for x in lst:
        ans_lst.append(x.replace('?','a'))
    ans = min(ans_lst)
print(ans)

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

Solution (Python):
import sys
import re
 
 
 
def check(Sda, T, Slen, Tlen):
    matarr=[]
    endarr=[]
 
    getS = 'UNRESTORABLE'
 
    if(Slen<Tlen):
        return getS
 
    for i in reversed(range(0, Slen-Tlen+1)):
 
 
        mat = re.match(Sda[i:i+Tlen],T)
        try:
            if(mat.group() == T):
                matarr.append(Sda[:i]+T+Sda[i+Tlen:])
 
        except AttributeError:
            pass
 
        mat = 'None'
 
    if(matarr != []):
        for x in matarr:
            x = x.replace('.', 'a')
            endarr.append(x)
 
        getS = min(endarr)
 
    return getS
 
 
 
def main():
    Sda = input()
    T = input()
    Slen = len(Sda)
    Tlen = len(T)
 
    Sda = Sda.replace('?', '.')
 
    print(check(Sda, T, Slen, Tlen))
 
 
main()

Solution (Python):
S = str(input())
T = str(input())
N = len(S)-len(T)+1
M = len(T)
li = []

for i in range(N):
    flag = 0
    for j in range(M):
        if S[i+j]==T[j] or S[i+j] == "?":
            flag += 1
        else:
            break
    if flag == M:
        ans = S[:i]+T+S[i+M:]
        ans = ans.replace('?','a')
        li.append(ans)

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

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

if len(s)<len(t):
    print("UNRESTORABLE")
    exit()

sub=[]
for i in range(len(s)-len(t)+1):
    t_in_s = False
    part_s = s[i:i+len(t)]
    for g in range(len(t)):
        if part_s[g]==t[g] or part_s[g]=="?":
            t_in_s=True
            continue
        else:
            t_in_s=False
            break
    if t_in_s is True:
        sub.append(s[:i]+t+s[i+len(t):])

if sub==[]:
    print("UNRESTORABLE")
    exit()

for l in range(len(sub)):
    sub[l]=sub[l].replace("?","a")

sub.sort()
print(sub[0])

Solution (Python):
sd = input()
t = input()
sl = len(sd)
tl = len(t)
pos = []
for i in range(sl-tl+1):
    tmp = sd[i:i+tl]
    n = 0
    for j, k in zip(tmp, t):
        if j == '?':
            n += 1
        elif j == k:
            n += 1
        else:
            break
    if n == tl:
        ans = sd[:i] + t + sd[i+tl:]
        ans = ans.replace('?', 'a')
        pos.append(ans)
pos.sort()
if len(pos) == 0:
    print('UNRESTORABLE')
else:
    print(pos[0])

Solution (Python):
import copy

s = input()
t = input()
n = len(t)

ans = set()
flg = False
for i in range(len(s)):
    tmp = s[i: i + n]
    for j in range(n):
        try:
            if tmp[j] != "?" and tmp[j] != t[j]:
                flg = False
                break
            else:
                flg = True
        except IndexError:
            flg = False
            break
    if flg:
        if len(ans) > 0:
            ans.clear()
        s_1_l = list(copy.deepcopy(s))
        for k in range(len(s_1_l)):
            if s_1_l[k] == "?":
                s_1_l[k] = "a"
        s_1_l[i: i + n] = t
        ans.add("".join(s_1_l))

if len(ans) == 0:
    print("UNRESTORABLE")
else:
    print(*ans)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""

S = gw()
T = gw()

lt = len(T)
ls = len(S)

found = 0
ans = S
for i in range(ls - lt, -1, -1):
    good = True
    for j in range(i, i + lt):
        good = S[j] == T[j - i] or S[j] == "?"
        if not good:
            break
    if good:
        found = 1
        ans = S[0:i] + T + S[i + lt:]
        break

if found:
    print(ans.replace("?", "a"))
else:
    print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
length = len(T)
results = []
for i in range(len(S)-length+1):
    flag = True
    for s, t in zip(S[i:i+length], T):
        if s == "?":
            continue
        if s != t:
            flag = False
            break
    if flag:
        results.append(S[:i] + T + S[i+length:])

if len(results) == 0:
    print("UNRESTORABLE")
else:
    results = [res.replace("?", "a") for res in results]
    results.sort()
    print(results[0])

Solution (Python):

S = input()
T = input()

len_t = len(T)
G = []

for i in range(len(S)-len_t+1):
  valid = True
  
  for j in range(len_t):
    if S[i+j] != "?" and S[i+j] != T[j]:
      valid = False

  if valid:
    SL = list(S)
    for j in range(len_t):
      SL[i+j] = T[j]
    G.append("".join(SL).replace("?", "a"))

print(min(G, default="UNRESTORABLE"))

Solution (Python):
S_prime = list(map(str, input()))
T = list(map(str, input()))

len_S = len(S_prime)
len_T = len(T)

un = 1

for i in reversed(range(0, len_S - len_T + 1)):
	match = 1
	for j in range(i, i + len_T):
		
		if S_prime[j] == "?": continue
		if not S_prime[j] == T[j - i]:
			match = 0
			break
	if match:
		un = 0
		
		init = i
		break
		
if un:
	print("UNRESTORABLE")
else:
	for i in range(init, init + len_T):
		S_prime[i] = T[i - init]
	
	for i in range(len_S):
		if S_prime[i] == "?":
			print("a", end = "")
		else:
			print(S_prime[i], end = "")

Solution (Python):
import sys
input = sys.stdin.readline
s=input().strip()
t=input().strip()
last=-1
lastsp=-1
for i in range(len(s)-len(t)+1):
    flg=True
    flgsp=True
    for j in range(len(t)):
        
        
        if s[i+j]==t[j]:
            continue
        else:
            flgsp=False

        if s[i+j]=="?":
            continue
        else:
            flg=False
    
    if flgsp:
        lastsp=i
    if flg:
        last=i
    
ans=""
if lastsp>=0:
    
    for i in range(len(s)):
        if s[i]!="?":
            ans+=s[i]
        else:
            ans+="a"
elif last>=0:
    
    for i in range(last):
        if s[i]!="?":
            ans+=s[i]
        else:
            ans+="a"
    ans+=t
    for i in range(last+len(t),len(s)):
        if s[i]!="?":
            ans+=s[i]
        else:
            ans+="a"
else:
    ans="UNRESTORABLE"
print(ans)

Solution (Python):
s_d=input()
t=input()
if len(s_d) < len(t):
    print("UNRESTORABLE")
    exit()
for i in range(len(s_d)-len(t),-1,-1):
    flag = True
    for j,k in zip(s_d[i:i+len(t)],t):
        if j != k:
            flag=False
            break
    if flag:
        s_d = s_d[:i]+t+s_d[i+len(t):]
        break
if not flag:
    for i in range(len(s_d) - len(t), -1, -1):
        flag = True
        for j, k in zip(s_d[i:i + len(t)], t):
            if j != "?" and j != k:
                flag = False
                break
        if flag:
            s_d = s_d[:i] + t + s_d[i + len(t):]
            break
if not flag:
    print("UNRESTORABLE")
else:
    for i in s_d:
        if i == "?":
            print("a",end="")
        else:
            print(i,end="")

Solution (Python):
def solve():
    from sys import stdin
    f_i = stdin
    
    Sd = f_i.readline().rstrip()
    T = f_i.readline().rstrip()
    
    for i in range(len(Sd) - len(T), -1, -1):
        for x, t in zip(Sd[i:], T):
            if x != '?' and x != t:
                break
        else:
            ans = Sd[:i] + T + Sd[i+len(T):]
            return ans.replace('?', 'a')
    
    return 'UNRESTORABLE'

print(solve())

Solution (Python):
from sys import exit

S = input()
T = input()


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


for i in range(len(S) - len(T), -1, -1):
    if match(S[i: i + len(T)], T):
        print((S[:i] + T + S[i + len(T):]).replace('?', 'a'))
        exit()
print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()
ans = 'UNRESTORABLE'
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:
        ans = S[:i].replace('?', 'a')+T+S[i+len(T):].replace('?', 'a')
        break
print(ans)

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

i = len(S) - 1
ti = len(T) - 1
match = 0
ms = len(S) - 1
flag = False
while 0 <= i:
    if S[i] == T[ti] or S[i] == '?':
        if ms == -1:
            ms = i
        match += 1
        ti -= 1
        i -= 1
    else:
        if match > 0:
            match = 0
            ti = len(T) - 1
            i = ms - 1
            ms = -1
        else:
            ms = -1
            match = 0
            ti = len(T) - 1
            i -= 1
    if match == len(T):
        k = 0
        i += 1
        for j in range(i, i + len(T)):
            S[j] = T[k]
            k += 1
        flag = True
        break




if flag == True:
    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'
    print(''.join(S))
else:
    print('UNRESTORABLE')

Solution (Python):
s = input()
t = input()
ls = len(s)
lt = len(t)
ans = "z"*(ls+1)

for i in range(ls-lt+1):
    for j in range(lt):
        if s[i+j] != t[j] and s[i+j] != "?":
            break
    else:
        tmp = (s[:i]+t+s[i+lt:]).replace("?","a")
        if ans > tmp:
            ans = tmp
else:
    if ans == "z"*(ls+1):
        ans = "UNRESTORABLE"

print(ans)

Solution (Python):
s = input()
t = input()
ls = len(s)
lt = len(t)
ans = "z"*ls

for i in range(ls-lt+1):
    for j in range(lt):
        if s[i+j] != t[j] and s[i+j] != "?":
            break
    else:
        tmp = (s[:i]+t+s[i+lt:]).replace("?","a")
        if ans > tmp:
            ans = tmp
else:
    if ans == "z"*ls:
        ans = "UNRESTORABLE"

print(ans)

Solution (Python):
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	
	sys.stdin = open(name)

def checkstr(i,j,s1,s2):
	
	if j == len(s2):
		
		return True
	if s1[i]=="?" or s1[i]==s2[j]:
		if checkstr(i+1,j+1,s1,s2):
			return True
	else:
		return False

s1 = input()
s2 = input()

j=0



if len(s1)<len(s2):
	print("UNRESTORABLE")
	exit()

for i in range(len(s1)-len(s2),-1,-1):
	
	if checkstr(i,j,s1,s2):
		
		s=s1[:i]+s2+s1[i+len(s2):]
		
		print(s.replace('?', 'a'))
		exit()
	if i==0:
		print("UNRESTORABLE")

Solution (Python):
S = input()
S = [e for e in S]
T = input()
T = [e for e in T]
restorable = False
for i in range(len(S)-len(T)+1)[::-1]:
    sub_S = S[i:i+len(T)]
    flag = True
    for s,t in zip(sub_S,T):
        if s != '?':
            if s != t:
                flag = False
                break
    if flag:
        restorable= True
        for j in range(i,i+len(T)):
            S[j] = T[j-i]
        break
if not restorable:
    print('UNRESTORABLE')
    exit()
for i in range(len(S)):
    if S[i] == '?':
        S[i] = 'a'
print("".join(S))

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


flag = False
for i in reversed(range(len(s)-len(t)+1)):
    count = 0
    for j in range(len(t)):
        if s[i+j] == t[j] or s[i+j] == '?':
            count += 1
        else:
            break
    if count == len(t):
        flag = True
        list_s = list(s)
        list_t = list(t)
        list_s[i:i+len(t)] = list_t
        s = ''.join(list_s).replace('?', 'a')
        break
if flag:
    print(s)
else:
    print('UNRESTORABLE')

Solution (Python):

S=input()
T=input()
ls=len(S)
lt=len(T)

if lt>ls:
    print('UNRESTORABLE')
    exit()

if S.count(T)>0:
    S=S.replace('?','a')
    print(S)
    exit()

for i in range(ls-lt+1):
    NG_flg=False
    t_str=S[ls-lt-i:ls-i]
    for j,tt in enumerate(t_str):
        if tt==T[j:j+1] or tt=='?':
            pass
        else:
            NG_flg=True
            break
    if NG_flg==False:
        S=list(S)
        T=list(T)
        for k in range(lt):
            S[ls-lt-i+k:ls-lt-i+1+k]=T[k:k+1]
        break

if NG_flg==True:
    print('UNRESTORABLE')
    exit()

ans=''
for ss in S:
    ans+=ss
ans=ans.replace('?','a')

print(ans)

Solution (Python):
import re

S = input()
Sa = S.replace("?", "a")
Sd = S.replace("?", ".")
T = input()

word_L = []
for i in range(len(S)-len(T)+1):
  if re.match(Sd[i:i+len(T)], T) is not None:
    s = Sa[:i] + T + Sa[i+len(T):]
    word_L.append(s)

ans = "UNRESTORABLE"
if word_L:
  ans = sorted(word_L)[0]
print(ans)

Solution (Python):
def is_word(i):
  
  sub_S = S[i:i+len(T)]
  for s, t in zip(sub_S, T):
    if s == "?":
      continue
    if s != t:
      return False
  return True

S = input()
rep_S = S.replace("?", "a")
T = input()

word_L = []
for i in range(len(S)-len(T)+1):
  if is_word(i):
    s = rep_S[:i] + T + rep_S[i+len(T):]
    word_L.append(s)

ans = "UNRESTORABLE"
if word_L:
  ans = sorted(word_L)[0]
print(ans)

Solution (Python):
s=list(input())
t=list(input())
for i in range(len(s)-len(t),-1, -1):
    for j in range(len(t)):
        if not (s[i+j] == t[j] or s[i+j] == '?'):
            break
    else:
        s[i:i+len(t)] = t
        for i in range(len(s)):
            if s[i] == '?':
                s[i] = 'a'
        print(''.join(s))     
        break
else:
    print('UNRESTORABLE')

Solution (Python):
s = input()
t = input()
flag = False
tq = '?'*len(t)
start = []
for i in range(len(s)-len(t)+1):
    flag = True
    for j in range(len(t)):
        if s[i+j] == t[j] or s[i+j] == '?':
            continue
        else:
            flag = False
            break
    if flag:
        start.append(i)
    
stal = []
if start:
    for i in start:
        stal.append(s[:i].replace('?','a') + t + s[i+len(t):].replace('?','a'))
    stal.sort()
    print(stal[0])
else:
    print('UNRESTORABLE')

Solution (Python):

def main():
  S_=input()
  T=input()
  ans=''
  if len(S_)<len(T):
    print('UNRESTORABLE')
    return
  for i in range(len(S_)-len(T)+1):
    flag=True
    for j in range(len(T)):
      if S_[i+j]!='?' and S_[i+j]!=T[j]:
        flag=False
        break
    if flag:
      S=['a']*len(S_)
      for k in range(i):
        if S_[k]!='?':
          S[k]=S_[k]
      for k in range(i,i+len(T)):
        S[k]=T[k-i]
      for k in range(i+len(T),len(S_)):
        if S_[k]!='?':
          S[k]=S_[k]
      if ans=='':
        ans=''.join(S)
      elif ans>''.join(S):
        ans=''.join(S)
  if ans=='':
    print('UNRESTORABLE')
  else:
    print(ans)
  return

main()

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

Solution (Python):


S_ = input()    
T = input()     


len_S_ = len(S_)
len_T = len(T)

S_list = []

for i in reversed(range(len_S_)):
    is_target = True
    S = ''

    if i + len_T <= len_S_:
        S = S_[:i] + T + S_[i + len_T:]

        
        for j in range(len(S)):
            if S[j] != S_[j] and S_[j] != '?':
                is_target = False
                break

            if S[j] == '?':
                S = S[:j] + 'a' + S[j + 1:]

        if is_target:
            S_list.append(S)

    else:
        continue
        
if len(S_list) > 0:
    print(min(S_list))
else:
    print('UNRESTORABLE')

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

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] == "?":
            pass
        else:
            break
        if j == len(T)-1:
            ans = S[:i] + T + S[i+len(T):]
            ans = ans.replace("?","a")
            print(ans)
            sys.exit()
print("UNRESTORABLE")

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

S1 = S[::-1]
T1 = T[::-1]
count = 0
for i in range(len(S)-len(T)+1):
    count = 0

    for j in range(len(T1)):
        
        if S1[i+j] == "?" or S1[i+j] == T1[j]:
            

            count += 1

    
    if count == len(T1):
        
        for k in range(len(T1)):
            S1[k+i] = T1[k]
            
        S2 = S1[::-1]

        for i in S2:
            if i == "?":
                print("a",end="")
            else:
                print(i,end="")
        exit()

print("UNRESTORABLE")

Solution (Python):


S = input()
T = input()

xS = S[::-1]
xT = T[::-1]

point = -1

for i in range(len(xS) - len(xT) + 1):
    for j in range(len(T)):
        if xS[i + j] == xT[j] or xS[i + j] == "?":
            if j == len(xT) - 1:
                point = i
                break
            continue
        else:
            break
    if point != -1:
        break


xS = list(xS)
for i in range(len(xT)):
    xS[i + point] = xT[i]

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

xS = xS[::-1]
if point != -1:
    print("".join(xS))
else:
    print("UNRESTORABLE")

Solution (Python):
s = str(input())
t = str(input())
s_l =[]
for i in range(len(s)):
    s_l.append(s[i])

if len(s) < len(t):
    print("UNRESTORABLE")
    exit()

a = len(s) - len(t)

while a >= 0:
    insert = False
    string = True
    i = 0
    while i < len(t):
        if s[a + i] != "?" and s[a + i] != t[i]:
            string = False
            break
        i += 1
        
    if string == True:
        for i in range(len(t)):
                s_l[a+i] = t[i]
        insert = True
        break
    a -= 1

f = ""
if insert == True:
    for i in range(len(s)):
        if s_l[i] == "?":
            f += "a"
        else:
            f += s_l[i]
    print(f)
else:
    print("UNRESTORABLE")

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = input()
n = len(s)
m = len(t)
flg = False
for i in range(m-1,n)[::-1]:
    if s[i] == t[-1] or s[i] == '?':
        cnt = 0
        while cnt < m:
            if s[i-cnt] == t[-(cnt+1)] or s[i-cnt] == '?':
                cnt += 1
                if cnt == m:
                    flg = True
            else:
                break

        if flg:
            break
if not flg:
    print('UNRESTORABLE')
    quit()
ans = ''
cnt2 = 0
for k in range(n):
    if i-cnt < k <= i:
        ans += t[cnt2]
        cnt2 += 1
    else:
        if s[k] == '?':
            ans += 'a'
        else:
            ans += s[k]
print(ans)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    T=ST()
    N=len(S)
    M=len(T)
    ans = []
    for i in range(N-M+1)[::-1]:
        can = True
        for s,t in zip(S[i:],T):
            if s!=t and s!="?":
                can = False
        if can:
            for j,s in enumerate(S):
                if j<i:
                    if s=="?":
                        ans.append("a")
                    else:
                        ans.append(s)
                else:
                    if len(T)>j-i:
                        ans.append(T[j-i])
                    else:
                        if s=="?":
                            ans.append("a")
                        else:
                            ans.append(s)
            break
    if ans:
        print(*ans,sep="")
    else:
        print("UNRESTORABLE")
if __name__ == '__main__':
    main()

Solution (Python):
S_=input()
T=input()
ns=len(S_)
nt=len(T)
cand=[]

def f(x, y):
  n=len(x)
  out=True
  for i in range(n):
    if x[i]!=y[i] and x[i]!='?':
      out=False
  return out

for i in range(ns-nt+1):
  if f(S_[i:i+nt], T)==True:
    temp=''
    for j in range(i):
      if S_[j]=='?':
        temp+='a'
      else:
        temp+=S_[j]
    temp+=T
    for j in range(i+nt, ns):
      if S_[j]=='?':
        temp+='a'
      else:
        temp+=S_[j]
        
    cand.append(temp)
    
cand.sort()
cand.append('UNRESTORABLE')
print(cand[0])