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 copy
S_original = input()
T = input()

lis_t = list(T)
T_size = len(T)
S_size = len(S_original)
hantei = 0
lis = []

if T_size <= S_size:
    for start_index in range(S_size - T_size + 1):

        S_prime = copy.deepcopy(S_original)
        hantei = 0
        for i in range(T_size):
            if S_prime[start_index+i] == T[i]:
                continue
            elif S_prime[start_index+i] == "?":
                s_lis = list(S_prime)
                s_lis[start_index+i] = lis_t[i]
                S_prime = "".join(s_lis)
            elif S_prime[start_index+i] != T[i]:
                hantei = 1
                break
        if hantei == 0:
            for j in range(S_size):
                if S_prime[j] != "?":
                    continue
                else:
                    s_lis = list(S_prime)
                    s_lis[j] = "a"
                    S_prime = "".join(s_lis)
            lis.append(S_prime)

if lis:
    lis = sorted(lis)
    print(lis[0])
else:
    print ("UNRESTORABLE")

Solution (Python):
S1=input()
T=input()
lens1=len(S1)
lent=len(T)
ans="z"*(lens1+1)
def OK(k):
    for i in range(lent):
        if S1[k+i]!=T[i] and S1[k+i]!="?":
            return False
    return True
for i in range(lens1-lent+1):
    if OK(i):
        Stmp=""
        for j in range(lens1):
            if i<=j<i+lent:
                Stmp+=T[j-i]
            elif S1[j]=="?":
                Stmp+="a"
            else:
                Stmp+=S1[j]
        if Stmp<ans:
            ans=Stmp
if ans!="z"*(lens1+1):
    print(ans)
else:
    print("UNRESTORABLE")

Solution (Python):


S_dash = input()
T = input()

S_dash_list = list(S_dash)
T_list = list(T)

ei = len(S_dash_list)
si = ei - len(T_list)

insert_flag = False
while si >= 0:
    
    target = S_dash_list[si:ei]
    flag = True
    for i in reversed(range(len(T_list))):
        if S_dash_list[si + i] != T_list[i] and S_dash_list[si + i] != '?':
            flag = False
            
            ei -= 1
            si = ei - len(T_list)
            break
    if flag:
        for i in range(len(T_list)):
            S_dash_list[si + i] = T_list[i]
        insert_flag = True
        break

if not insert_flag:
    print('UNRESTORABLE')
else:
    ans = ''.join(S_dash_list)
    

    
    print(ans.replace('?', 'a'))

Solution (Python):
def search(S,T):
    match = 0
    maxmatch = 0
    maxmatchloc = -1
    for x in range(len(S)-(len(T)-1)):
        match = 0
        if (S[x] == T[0] or S[x] == '?'):
            for i in range(0,len(T)):
                
                if (S[x+i] == T[i] or S[x+i] == "?"):
                    match += 1
                else:
                    
                    match = 0
                    break
            if (match >= maxmatch and match != 0):
                
                maxmatchloc = x
                maxmatch = match
        
    return maxmatchloc

S = input()
T = input()
code = S
if '?' in S:
    if (len(T) <= len(S)):
        startpoint = search(S,T)
        for x in range(len(S)):
            if (S[x] == '?'):
                code = code[:x] + 'a' + code[x+1:] 
        if(startpoint >= 0):
            code = code[:startpoint] + T + code[startpoint + len(T):]
            print(code)
        else:
            print('UNRESTORABLE')
    else:
        print('UNRESTORABLE')
else:
    print(S)

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

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

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

Solution (Python):
s=list(input())
t=list(input())
ans=[0 for _ in range(len(s))]
for i in range(len(s)-len(t)+1):
    cnt=0
    for j in range(len(t)):
        if s[i+j]=='?' or s[i+j]==t[j]:
            cnt+=1
    if cnt==len(t):
        ans[i]=i+1
mark=ans.index(max(ans))
if sum(ans)!=0:
    for j in range(len(s)):
        if mark<=j and j<mark+len(t):
            s[j]=t[j-mark]
        elif s[j]=='?':
            s[j]='a'
    print(''.join(s))
else:print("UNRESTORABLE")

Solution (Python):
import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def check(sp, s):
    
    for i in range(len(s)):
        if sp[i] != '?' and s[i] is not None and sp[i] != s[i]:
            return False
    return True

def main():
    sp = input()
    n = len(sp)
    t = input()
    s = [None] * n

    for i in range(n):
        if sp[i] != '?':
            s[i] = sp[i]
    anss = list()
    
    for i in range(n-len(t)+1):
        p = [None]*n
        for j in range(len(t)):
            p[i+j] = t[j]
        if check(sp, p):
            ans = str()
            for i in range(n):
                if p[i] is None:
                    if sp[i] == '?':
                        ans += 'a'
                    else:
                        ans += sp[i]
                else:
                    ans += p[i]
            anss.append(ans)
    if anss:
        anss.sort()
        print(anss[0])
        return
    print('UNRESTORABLE')
    return
            

    

if __name__ == '__main__':
    main()

Solution (Python):


import sys

DEBUG = False

def read_int_list(sep = " "):
    return [int(s) for s in sys.stdin.readline().rstrip().split(sep)]

def read_int():
    return int(sys.stdin.readline())

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return

def solve(s, t):
    if len(t) > len(s):
        return "UNRESTORABLE"

    ans = None
    for i in range(0, len(s)):
        can_insert = True
        for j in range(0, len(t)):
            pos = i + j
            if pos >= len(s) or (s[pos] != '?' and s[pos] != t[j]):
                can_insert = False
                break
        if not can_insert:
            continue
        inserted = s[0:i] + t + s[i + len(t):]
        inserted = inserted.replace("?", "a")

        if ans is None or inserted < ans:
            ans = inserted

    if ans is None:
        return "UNRESTORABLE"
    return ans


def main():
    s = sys.stdin.readline().rstrip()
    t = sys.stdin.readline().rstrip()
    print(solve(s, t))
    

if __name__ == "__main__":
    main()

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

ans1 = "z"*len(S)
pos = ""
num = 0
for i in range(len(S)-len(T)+1):
    sl = ""
    sl = S[i:(i+len(T))]
    for j in range(len(T)):
        pos = ""
        if(sl[j] != "?" and sl[j] != T[j]):
            break
    else:
        pos += S[:i]+T+S[(i+len(T)):]
        if(ans1 > pos):
            ans1 = pos
            Flg = True
                
if(Flg == True):
    ans1 = ans1.replace("?", "a") 
    print(ans1)
else:
    print("UNRESTORABLE")

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

Solution (Python):
def check(a, b):
    for j in range(len(a)):
        if b[j] == a[j] or a[j] == '?':
            continue
        else:
            return False
    return True

ans_list = []
s_prime = input()
t = input()
for i in range(len(s_prime) - len(t) + 1):
    if check(s_prime[i:i+len(t)], t):
        s = s_prime[:i] + t + s_prime[i+len(t):]
        ans = s.replace('?', 'a')
        ans_list.append(ans)
ans_list.sort()
if len(ans_list) > 0:
    print(ans_list[0])
else:
    print('UNRESTORABLE')

Solution (Python):
s = list(input())
t = list(input())
bag = []
for i in range(len(s)-len(t)+1):
  st = False
  for j in range(len(t)):
    if j==0 and (s[i+j]==t[j] or s[i+j]=='?'):
      st = True 
    elif st==True and (s[i+j]==t[j] or s[i+j]=='?'):
      pass 
    elif st==True and (s[i+j]!=t[j] and s[i+j]!='?'):
      st = False 
      break 

  if st:
    bag += [i]

if len(bag)==0:
  print('UNRESTORABLE')
  exit()
else:
  idx = max(bag)
  s[idx:idx+len(t)] = t 

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

print(''.join(s))

Solution (Python):
b = list(input())
t = list(input())
kouho = []
if len(b) < len(t):
  print("UNRESTORABLE")
else:
  for i in range(len(b)-len(t)+1):
    a = b[:]
    ok = True
    for j in range(len(t)):
      if a[i+j] == "?" or a[i+j] == t[j]:
        a[i+j] = t[j]
      else:
        ok = False
    if ok:
      for j in range(len(a)):
        if a[j] == "?":
          a[j] = "a"
      kouho.append("".join(a))
  if kouho:
    kouho.sort()
    print(kouho[0])
  else:
    print("UNRESTORABLE")

Solution (Python):
import numpy as np
S = np.array(list(input()))
T = np.array(list(input()))
for i in range(len(S), -1, -1):
    subset = S[i:(i + len(T))]
    if len(subset) == len(T):
        
        
        
        if np.all((subset == '?') | (subset == T)):
            S[i:(i + len(T))] = T
            S[S == '?'] = 'a'
            
            print(''.join(S))
            break
else:
    print('UNRESTORABLE')

Solution (Python):
def matching(s,t):
    ret = True
    for s_, t_ in zip(s,t):
        if not (s_ == t_ or s_ == '?'):
            ret = False
            break
            
    return ret

def solve():
    S = input()
    T = input()
    L_S = len(S)
    L_T = len(T)
    pos = -1
    match = False
    for i in range(L_S-L_T+1)[::-1]:
        if matching(S[i:i+L_T],T):
            pos = i
            break
            
    if pos >= 0:
        result = ''.join(list(map(lambda x: 'a' if x=='?' else x, S)))
        print(result[:pos]+T+result[pos+L_T:])
    else:
        print('UNRESTORABLE')
        
solve()

Solution (Python):
import re

S = input()
T = input()

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

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

n = len(S)-len(T)+1
ans = []
for i in range(n):
    SS = list("?"*(i) + T + "?"*(n-i-1))
    flag = True
    for i in range(len(S)):
        if S[i]=="?" or S[i]==SS[i]:
            pass
        elif S[i]!="?" and SS[i]=="?":
            SS[i]=S[i]
        else:
            flag = False
    if flag:
        ans.append("".join(SS).replace("?","a"))

if ans:
    ans.sort()
    print(ans[0])
else:
    print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
koho = []

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j] != '?' and S[i+j] != T[j]:
            break
    else:
        koho.append(S[:i]+T+S[i+len(T):])
result = sorted([s.replace('?', 'a') for s in koho])
print('UNRESTORABLE' if result == [] else result[0])

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

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

for i in range(len(s) - len(t), -1, -1):
  if not match(i):
    continue

  for j in range(i):
    print('a' if s[j] == '?' else s[j], end='')
  print(t, end='')
  for j in range(i+len(t), len(s)):
    print('a' if s[j] == '?' else s[j], end='')
  print()
  exit()
    
print('UNRESTORABLE')

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

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

s1 = len(s)
t1 = len(t)

ans = []

for i in range(s1 - t1 + 1):
    s_tmp = s[i:i+t1]
    flag = True
    for j in range(t1):
        if s_tmp[j] != '?' and s_tmp[j] != t[j]:
            flag = False
    if flag:
        s_tmp2 = s[:i] + t + s[i+t1:]
        s_tmp3 = ''
        for j in range(s1):
            if s_tmp2[j] == '?':
                s_tmp3 += 'a'
            else:
                s_tmp3 += s_tmp2[j]
        ans.append(s_tmp3)

if ans:
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

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

S_l = len(Sd)
T_l = len(T)

ans_list = []

for i in range(S_l-T_l+1):
    flag = 0
    for j in range(T_l):
        if Sd[i+j] != '?':
            if Sd[i+j] != T[j]:
                flag = 1
                break

    if flag == 0:
        ans = Sd[:i] + T + Sd[i+T_l:]
        ans_list.append(ans.replace('?', 'a'))

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

Solution (Python):
S = list(input())
T = list(input())
sl = len(S)
tl = len(T)




flag = False
for i in range(sl - tl, -1, -1):
    P = S[i:i+tl]
    for p, t in zip(P, T):
        if p != '?' and p != t:
            break
    else:  
        S[i:i+tl] = T
        flag = True
        break

if flag:        
    print(''.join(['a' if s == '?' else s for s in S]))
else:
    print('UNRESTORABLE')

Solution (Python):
import re

sp=input()
t=input()

strlst=[]

def makeStrLex(i):
    s=sp[:i]+t+sp[i+len(t):]
    s=s.replace('?','a')

    return s

for i in range(len(sp)-len(t)+1):
    spat=sp[i:i+len(t)].replace('?','.')
    repat=re.compile(spat)
    
    if repat.match(t):
        strlst.append(makeStrLex(i))

if len(strlst)>0:
    print(sorted(strlst)[0])
else:
    print('UNRESTORABLE')

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    s = readline().strip()
    t = readline().strip()
    ls = len(s)
    lt = len(t)

    if ls < lt:
        print("UNRESTORABLE")
        exit()

    ind = -1
    for i in range(ls-lt+1):
        flag = True
        for j,t_ in enumerate(t):
            if s[i+j] == "?":
                continue
            if t_ != s[i+j]:
                flag = False
        if flag:
            ind = i
    
    if ind == -1:
        print("UNRESTORABLE")
        exit()
    
    ans = s[:ind] + t + s[ind+lt:]
    ans = ans.replace("?", "a")
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
S=input()
T=input()
N=len(S)
n=len(T)
core=[]
for i in range(N-n,-1,-1):
    S1=S
    time=0
    for j in range(i,i+n):
        if S[j]=="?":
            time+=1
            continue
        elif S[j]==T[j-i]:
            time+=1
            continue
        else:
            break
    if time==n:
        S1=S1[:i]+T+S1[i+n:]
        S1=S1.replace("?","a")
        core.append(S1)

if len(core)==0:
    print("UNRESTORABLE")
else:
    core.sort()
    print(core[0])

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

n = len(s)
m = len(t)

s.reverse()
t.reverse()

ans = []
for i in range(n-m+1):
    flg = True
    for j in range(m):
        if s[i+j] != '?' and s[i+j] != t[j]:
            flg = False

    if flg == True:
        s2 = s
        for j in range(n):
            if s[j] == '?':
                s2[j] = 'a'
        for j in range(m):
            s2[i+j] = t[j]
        ans.append(s2)
    

ans.sort()
if len(ans) == 0:
    print('UNRESTORABLE')
else:
    print(''.join(reversed(ans[0])))

Solution (Python):
s = input()
t = input()
for i in range(len(s)-len(t),-1,-1):
	
	flg = True
	for j, k in enumerate(list(t)):
		
		if s[i:i+len(t)][j] == "?":
			continue
		if k != s[i:i+len(t)][j]:
			flg = False
			break
	if flg == True:
		x = s[:i].replace("?","a") + t + s[i+len(t):].replace("?","a")
		print (x)
		exit()
print ("UNRESTORABLE")

Solution (Python):
import sys
from itertools import permutations
import math
from operator import itemgetter

s=input()
t=input()

if len(s)<len(t):
    print('UNRESTORABLE')
    sys.exit()

flag2=True
for i in reversed(range(len(s)-len(t)+1)):
    if (s[i]==t[0]) | (s[i]=='?'):
        flag=True
        for j in range(1,len(t)):
            if (s[i+j]!=t[j]) & (s[i+j]!='?'):
                flag=False
        if flag:
            s=s[:i]+t+s[i+len(t):] 
            s=s.replace('?','a')
            print(s)
            flag2=False
            sys.exit()

if flag2:
    print('UNRESTORABLE')

Solution (Python):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
import copy

def main():
    mod=10**9+7
    S=list(input())
    T=list(input())
    ns=len(S)
    nt=len(T)
    
    ans=[]
    
    
    def match(n):
        for i in range(nt):
            
            if S[n+i]!=T[i]:
                if S[n+i]!="?":
                    return False
                    
        return True
    
    
    def make(n):
        temp=copy.deepcopy(S)
        for i in range(nt):
            temp[n+i]=T[i]
        for i in range(ns):
            if temp[i]=="?":
                temp[i]="a"
               
        a=(''.join(map(str, temp)))
        return a
    
    
    
    for i in range(ns-nt+1):
        if match(i):
            ans.append(make(i))
            
    if len(ans)!=0:      
        ans.sort()
        print(ans[0])
    else:
        print("UNRESTORABLE")
        

    

main()

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


for s in range(len(S)-1, len(T)-2, -1):
  
  if (S[s] == T[-1]) or S[s] == '?':
    
    
    for t in range(1,len(T),1):
      if (S[s-t] != T[-t-1]) and S[s-t] != '?':
        break
    
    else:
      
      S = S.replace('?', 'a')
      
      ans = S[:s-len(T)+1] + T + S[s+1:]
      
      
      break
else:
  ans = 'UNRESTORABLE'

print(ans)

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

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

ans=[]
for i in range(len(s)-len(t)+1):
    kouho=[]
    match=True

    for pre in range(i):
        if s[pre]=="?":
            kouho.append("a")
        else:
            kouho.append(s[pre])

    for j in range(len(t)):
        if s[i+j]=="?":
            kouho.append(t[j])
        elif s[i+j] == t[j]:
            kouho.append(t[j])
        else:
            match=False
            break

    for post in range(i+j+1,len(s)):
        if s[post]=="?":
            kouho.append("a")
        else:
            kouho.append(s[post])
    if match:
        ans.append(kouho)

if len(ans)==0:
    print("UNRESTORABLE")
else:
    ans.sort()
    print(*ans[0],sep="")

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

ls = len(sp)
lt = len(t)
ok = 1
s = list(sp)
for i in range(ls-1, -1, -1):
    ok = 1
    for j in range(lt):
        if i + j >= ls:
            ok = 0
            break
        if (sp[i+j] == t[j]) or (sp[i+j] == '?'):
            continue
        ok = 0
        break
    if ok == 1:
        for si in range(i, i+lt):
            s[si] = t[si - i]
        break

if ok == 1:
    print(''.join(s).replace("?", "a"))
else:
    print("UNRESTORABLE")

Solution (Python):



def solve(*args: str) -> str:
    S, T = args
    candidates = []
    for i in range(len(S)-len(T)+1):
        text = S[:i]

        for j in range(len(T)):
            if S[i+j] != T[j] and S[i+j] != '?':
                text = ''
                break
            text += T[j]

        if text:
            text += S[len(text):]
            candidates.append(text.replace('?', 'a'))

    candidates.sort()

    return candidates[0] if candidates else 'UNRESTORABLE'


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
def main():
    s=list(input())
    t=list(input())
    n=len(s)
    l=len(t)
    
    judge=False
    for i in range(n-l,-1,-1):
        flag=True
        for j,st in enumerate(s[i:i+l]):
            if st!='?' and st!=t[j]:
                flag=False
                break
        if flag:
            s[i:i+l]=t
            judge=True
            break
    
    if judge:
        for i in range(n):
            if s[i]=='?':
                s[i]='a'
        print(''.join(s))
    else:
      print('UNRESTORABLE')
        
if __name__=='__main__':
    main()

Solution (Python):
import copy
def solve():
    S_d = list(input())
    T = list(input())

    end = len(S_d) - 1
    first = end - len(T) + 1
    while first >= 0:
        t_i = 0 
        for s_i in range(first, end+1):
            if T[t_i] != S_d[s_i] and S_d[s_i] != '?':
                break
            t_i += 1
        else:
            break

        first -= 1
        end -= 1

    else:
        print('UNRESTORABLE')
        exit(0)
    
    ans = copy.deepcopy(S_d)
    t_i = 0
    for i in range(first, end+1):
        ans[i] = T[t_i]
        t_i += 1
    
    ans = ''.join(ans).replace('?','a')
    print(ans)

if __name__ == '__main__':
    solve()

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

ans = []

for index, s in enumerate(S):
  if s == '?' or s == T[0]:
    if index+len(T)-1 > len(S)-1:
        break

    for indexs, t in enumerate(T):
      if S[index+indexs] != t and S[index+indexs] != '?':
        break
    else:
      ans.append(S[0:index]+T+S[index+len(T):])


if len(ans) == 0:
  print("UNRESTORABLE")
else:
  ans.sort()
  print(ans[0].replace('?', 'a'))

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

LEN_S = len(S)
LEN_T = len(T)

if LEN_S < LEN_T:
  print('UNRESTORABLE')
  exit()

def isEquals(s):
  for i in range(LEN_T):
    if T[i] == s[i] or s[i] == '?':
      pass
    else:
      return False
  return True

answers = []
for i in range(LEN_S - LEN_T+1):
  if isEquals(S[i:i+LEN_T]):
    text = S[:i].replace('?', 'a') + T + S[i+LEN_T:].replace('?', 'a')
    answers.append(text)
  else:
    pass

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

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):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60


s = input()
t = input()
index = INF
for i in reversed(range(len(s) - len(t) + 1)):
    flag = True
    for j in range(i, i + len(t)):
        if s[j] != "?" and s[j] != t[j - i]:
            flag = False
    if flag:
        index = i
        break
    if flag:
        break
if index == INF:
    print("UNRESTORABLE")
else:
    s = s[:index] + t + s[index + len(t):]
    s = s.replace("?", "a")
    print(s)

Solution (Python):
def check(a, b):
    for i, j in zip(a, b):
        if i == "?" or i == j:
            pass
        else:
            return False
    return True


def make(a):
    x = ""
    for i in a:
        if i == "?":
            x += "a"
        else:
            x += i
    return x


s = list(input())
t = list(input())
n = len(t)
ans = []
for i in range(len(s)-n+1):
    if check(s[i:n+i], t):
        w = s[:i] + t + s[n+i:]
        ans.append(make(w))

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

Solution (Python):
def main():
    key_str = input()
    substring = input()
    answer = "z" * len(key_str)
    for i in range(len(key_str) - len(substring) + 1):
        if key_str[i] == substring[0] or key_str[i] == "?":
            for j in range(len(substring)):
                if not key_str[i + j] == substring[j] and not key_str[i + j] == "?":
                    break
            else:
                answer = min((key_str[:i] + substring + key_str[i + len(substring):]).replace("?", "a"), answer)
    print(answer if not answer == "z" * len(key_str) else "UNRESTORABLE")


if __name__ == '__main__':
    main()

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

ls = len(s)
lt = len(t)
match = -1

for i in range(ls-lt+1):
    for j in range(lt):
        if s[i+j] == t[j] or s[i+j] == '?':
            pass
            
        else:
            break
            
        if j == lt - 1:
            match = i
        
for i in range(ls):
    if s[i] == '?':
        s[i] = 'a'
        
for i in range(lt):
    s[match+i] = t[i]
        
if match < 0:
    print("UNRESTORABLE")
else:
    print(''.join(s))

Solution (Python):
s = input()[::-1]
t = input()[::-1]

if len(s) < len(t):
  print('UNRESTORABLE')
  exit(0)

ans = ''
for i in range(len(s) - len(t) + 1):
  for j in range(len(t)):
    if s[i+j] in ('?',t[j]):
      continue
    else:
      break
  else:
    ans = s[:i] + t + s[i+len(t):]
    break
else:
  print('UNRESTORABLE')
  
for c in ans[::-1]:
  print('a' if c=='?' else c,end= '')
print()

Solution (Python):
s=input()
t=input()
s_len=len(s)
t_len=len(t)
s=s[::-1]
t=t[::-1]
ans="UNRESTORABLE"
if s_len>=t_len:
    for i in range(s_len-t_len+1):
        count=0
        for j in range(t_len):
            if s[i+j]=="?" or s[i+j]==t[j]:
                count+=1
            else:
                break
        if count==t_len:
            s=s[:i]+t[:]+s[i+t_len:]
            s=s.replace("?", "a")
            s=s[::-1]
            ans=s
            break
print(ans)

Solution (Python):
S=input()
T=input()
def condition(str,num):
    flag=0
    for i in range(num,num+len(str)):
        if S[i]!='?' and S[i]!=str[i-num]:
            flag=1
    return flag==0
data=[]
for i in range(0,len(S)-len(T)+1):
    if condition(T,i):
        data.append(i)
if data!=[]:
    m=max(data)
    ans=''
    for i in range(0,len(S)):
        if i<m:
            if S[i]!='?':
                ans+=S[i]
            else:
                ans+='a'
        elif m+len(T)-1>=i:
            ans+=T[i-m]
        else:
            if S[i]!='?':
                ans+=S[i]
            else:
                ans+='a'
    print(ans)
else:
    print('UNRESTORABLE')

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

fir = -1
for i in range(len(s) - len(t) + 1):
    tmp = i
    flg = True
    for j in range(len(t)):
        if s[i + j] == "?" or s[i + j] == t[j]:
            continue
        else:
            flg = False
            break
    if flg:
        fir = tmp
if fir == -1:
    print("UNRESTORABLE")
else:
    ans = s[:fir] + t + s[fir + len(t):]
    print(ans.replace("?", "a"))

Solution (Python):

def solve():
    input_string = input()
    part_string = input()

    for i in range(len(input_string) - len(part_string), -1, -1):
        possible_part_string = input_string[i:i+len(part_string)]  
        for j in range(len(part_string) + 1):  
            if j == len(part_string):
                result = input_string[:i] + part_string + input_string[i + len(part_string):]
                result = result.replace("?", "a")
                print(result)
                return  
            if possible_part_string[j] == "?" or possible_part_string[j] == part_string[j]:
                continue
            else:  
                break

    print("UNRESTORABLE")
solve()

Solution (Python):
def solve():
    input_string = input()
    part_string = input()

    for i in range(len(input_string) - len(part_string), -1, -1):
        possible_part_string = input_string[i:i+len(part_string)]
        for j in range(len(part_string) + 1):  
            if j == len(part_string):
                result = input_string[:i] + part_string + input_string[i + len(part_string):]
                result = result.replace("?", "a")
                print(result)
                return
            if possible_part_string[j] == "?":
                continue
            elif possible_part_string[j] != part_string[j]:
                break

    print("UNRESTORABLE")
solve()

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=str(input())
t=str(input())
s=list(s)
t=list(t)
count2=0
count=0
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]:
            count=0
            break
        else:
            count+=1
        
    
    if count==len(t):
        for z in range(len(s)):
            if z<i or i+len(t)-1<z:
                if s[z]=='?':
                    print("a",end="")
                else:
                    print(s[z],end="")
            else:
                
                print(t[count2],end="")
                count2+=1
        exit()
    count=0
print("UNRESTORABLE")

Solution (Python):
def main():
  
  S=input()
  T=input()
  
  check=False
  ind=[]
  for i,l in enumerate(S):
    if i+len(T)>len(S):
      break
    for j,m in enumerate(T):
      if S[i+j]==m or S[i+j]=="?":
        check=True
      else:
        check=False
        break
    if check==True:
      ind.append(i)
  
  word=[]
  for i in ind:
    w=(S[:i]+T+S[i+len(T):]).replace("?","a")
    word.append(w)
  word.sort()
  
  if len(word)==0:
    print("UNRESTORABLE")
  else:
    print(word[0])

if __name__=="__main__":
  main()

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

slen = len(s)
Tlen = len(T)
ans = "UNRESTORABLE"
if slen >= Tlen:
    for i in range(slen - Tlen + 1)[::-1]:
        for j in range(Tlen):
            if not s[i+j] in ("?", T[j]):
                break
        else:
            ans = s
            ans = ans[:i] + T + ans[i+Tlen:]
            ans = ans.replace("?", "a")
            break

print(ans)