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):
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):
def solve():
    S = input()
    T = input()
    ls = len(S)
    lt = len(T)
    lst = []
    w_lst = []
    if ls < lt:
        return 'UNRESTORABLE'
    if ls == lt:
        for i in range(ls):
            if S[i] == '?': continue
            elif not S[i] == T[i]: return 'UNRESTORABLE'
        return T
    for d in range(ls - lt + 1):
        eq = True
        for i in range(lt):
            if S[i + d] == '?':
                continue
            elif not S[i + d] == T[i]:
                eq = False
                break
        if eq:
            lst.append(d)
    if len(lst) == 0:
        return 'UNRESTORABLE'
    for d in lst:
        w = S[0:d] + T + S[d + lt:]
        for i in range(ls):
            if w[i] == '?':
                w = w[0:i] + 'a' + w[i + 1:]
        w_lst.append(w)
    return min(w_lst)
if __name__ == '__main__':
    print(solve())

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

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

for i in range(len(S)):
    match = True
    for j in range(len(T)):
        if i + j < len(S):
            if not (T[j] == S[i + j] or S[i + j] == '?'):
                match = False
        else:
            match = False
    if match:
        for j in range(len(T)):
            S = S[:i + j] + T[j] + S[i + j + 1:]
        break
else:
    print('UNRESTORABLE')
    sys.exit()

S = S.replace('?', 'a')
print(S[::-1])

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD

S = input()
T = input()

ans = ''

index = -1
matched = False
for i in range(len(S)-len(T), -1, -1):
    for j in range(len(T)):
        if S[i+j] != T[j] and S[i+j] != '?':
            break
    else:
        matched = True
        S = S[0:i] + T + S[i+len(T):len(S)]
        break
if not matched:
    print('UNRESTORABLE')
else:
    for i in range(len(S)):
        if S[i] == '?':
            ans += 'a'
        else:
            ans += S[i]
    print(ans)

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

L_S = len(SS)
L_T = len(T)

COND = 0
ANS = []
for i in range(L_S-L_T+1):
	cond = 1
	t = ('?' * i) + T + ('?' * (L_S-L_T-i))

	ans = ''
	for j in range(L_S):
		if SS[j] == '?' and t[j] == '?':
			ans += 'a'
			
		elif SS[j] == '?' and t[j] != '?':
			ans += t[j]
		elif SS[j] != '?' and t[j] == '?':
			ans += SS[j]
		else:
			if SS[j] != t[j]:
				cond = 0
				ans = ''
				break
			else:
				ans += t[j]

	if cond == 0:
		continue
	else:
		ANS.append(ans)

if len(ANS) == 0:
	print('UNRESTORABLE')
else:
	ANS.sort()
	print(ANS[0])

Solution (Python):
S=list(input())
T=list(input())
p=-1; i=0; e=0
for i in range(len(S)-len(T)+1):
  for j in range(len(T)):
    if S[j+i]==T[j] or S[j+i]=="?":
      e=e+1
      
    else:
      e=0
      break
    if e==len(T):
      p=i
      e=0
    
if p==-1:
  print("UNRESTORABLE")
else:
  S[p:p+len(T)]=T
  for i in range(len(S)):
    if S[i]=="?":
      S[i]="a"
  print("".join(S))

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

num_t=len(t)
num=[]
for i in range(len(s)-num_t+1):
    temp_s=s[i:i+num_t]
    trial=True
    for x,y in zip(temp_s,t):
        if x!='?' and x!=y:
            trial=False
            break

    if trial:
        num+=[i]


if num==[]:
    print('UNRESTORABLE')
    exit()

ans='z'*50
for j in num:
    temp=''
    for i in range(len(s)):
        if j==i:
            temp+=t

        elif i<j or j+num_t<=i:
            if s[i]=='?':
                temp+='a'
            else:
                temp+=s[i]
    if temp<ans:
        ans=temp

print(ans)

Solution (Python):
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    ans = ""
    for i, s in enumerate(S[::-1]):
        cur = S[-(M+i):-i] if i != 0 else S[(-(M+i)):]
        if len(cur) == M:
            pass
        else:
            continue
        if (s == T[-1] or s == "?") and \
                all(ss == "?" or ss == t for ss, t in zip(cur, T)):
            if i != 0:
                ans = S[:-(M+i)] + T + S[-i:]
            else:
                ans = S[:-(M+i)] + T
            break
    if ans:
        ans = ans.replace("?", "a")
        print(ans)
    else:
        print("UNRESTORABLE")


if __name__ == '__main__':
    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):
import re

def solve(s, t):
    s = s.replace('?', '.')
    ls, lt = len(s), len(t)

    if(ls < lt):
        return 'UNRESTORABLE'

    ans = []
    for i in range(ls - lt + 1):
        result = re.match(s[i:i + lt], t)
        if result is None:
            continue
        ans.append((s[:i] + t + s[i + lt:]).replace('.', 'a'))

    if not ans:
        return 'UNRESTORABLE'
    return min(ans)

s = input()
t = input()

print(solve(s, t))

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

capable = []
for i in range(len(s) - len(t) + 1):
    restore = s.copy()
    flg = True
    for i_o, idx in enumerate(range(i, i + len(t))):
        if s[idx] == '?' or s[idx] == t[i_o]:
            restore[idx] = t[i_o]
            continue
        else:
            flg = False
    if flg:
        capable.append(restore)

if len(capable):
    print(''.join(capable[-1]).replace('?', 'a'))
else:
    print('UNRESTORABLE')

Solution (Python):
import sys

s = list(input())
t = list(input())
m = len(s) - len(t)

for i in range(m + 1):
    ok = True
    for j in range(len(t)):
        if s[m - i + j] != '?' and s[m - i + j] != t[j]:
            ok = False
            break
    if ok:
        s[m - i : m - i + len(t)] = t
        for j in range(len(s)):
            if s[j] == '?':
                s[j] = 'a'
        print(''.join(s))
        sys.exit()

print("UNRESTORABLE")

Solution (Python):
Sd=input()
T=input()
lS=len(Sd)
lT=len(T)
ret =[]
for i in range(lS-lT+1):
  S=list(Sd[:])
  Td='?'*i+T+'?'*(lS-lT-i)
  for j in range(lS):
    if Sd[j]=='?' and Td[j]=='?':
      S[j]='a'
    elif Sd[j]=='?' and Td[j]!='?':
      S[j]=Td[j]
    elif Sd[j]!='?' and Td[j]=='?':
      continue
    elif Sd[j]!='?' and Td[j]!='?':
      if Sd[j]!=Td[j]:
        S[j]='X'
        break
      else:
        continue
  if not 'X' in S:
    ret.append(''.join(S))
if len(ret)==0:
  print('UNRESTORABLE')
else:
  ret.sort()
  print(ret[0])

Solution (Python):
s=list(input())
t=input()
n,m=len(s),len(t)
def match(a,b):
    for i in range(len(a)):
        if a[i]!=b[i] and a[i]!="?":
            return False
    return True
flag=True
for i in range(n-m,-1,-1):
    if match(s[i:i+m],t):
        for j in range(m):
            s[j+i]=t[j]
        flag=False
        break
if flag:
    print("UNRESTORABLE")
    exit()
for i in range(n):
    if s[i]=="?":
        s[i]="a"
print(*s,sep="")

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

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

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

diff = ls - lt

candidates = []
for i in range(diff+1):
    string = S[i:i+lt]

    for j in range(lt):
        if string[j] == T[j] or string[j] == '?':
            continue
        else:
            break

    else:
        candidate = S[:i] + T + S[i+lt:]
        candidate = candidate.replace('?', 'a')
        candidates.append(candidate)

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

ans = min(candidates)
print(ans)

Solution (Python):
s = input()
t = input()
sl = len(s)
tl = len(t)

if sl < tl:
  print("UNRESTORABLE")
  exit()
  
possible = []
for i in range(sl - tl + 1):
  temp = s[i:i+tl]
  flag = True
  for a, b in zip(t, temp):
    if b != "?" and a != b:
      flag = False
  if flag:
    w = s[:i] + t + s[i+tl:]
    possible.append(w.replace("?", "a"))
if len(possible) == 0:
  print("UNRESTORABLE")
  exit()
print(sorted(possible)[0])

Solution (Python):
S = input()
T = input()
N = len(S)
check = [1]*len(S)




for i in range(len(S)):
    for j in range(len(T)):
        if (i+j)>=len(S):
            check[i]=0
            break
        if S[i+j]=="?":
            continue
        if S[i+j] != T[j]:
            check[i] = 0
            break
snum=-1
for i in range(N):
    if check[i]==1:
        snum=max(snum,i)

if snum==-1:
    print("UNRESTORABLE")
    exit()
s = []
for i in range(N):
    s.append(S[i])
for i in range(len(T)):
    s[snum+i]=T[i]

for i in range(N):
    if s[i]=="?":
        s[i] = "a"
ans = ""
for i in range(N):
    ans = ans+s[i]

print(ans)

Solution (Python):
import cmath
def match(st,tt):
    for i,j in zip(st,tt):
        
        if i!="?" and i!=j:
            return False
    return True

s=input()
t=input()
lens=len(s)
lent=len(t)
list=[]
for i in range(lens-lent+1):
    st=s[i:i+lent]
    
    if match(st,t)==True:
        ss=s[:i]+t+s[i+lent:]
        
        list.append(ss.replace("?","a"))
list.sort()
if list!=[]:
    print(list[0])
else:
    print("UNRESTORABLE")

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

Solution (Python):
s = input()
t = input()
y = []
for i in range(len(s)-len(t)+1):
    si = s
    for j in range(len(t)):
        if si[i+j] == '?':
            si = si[:i+j] + t[j] + si[i+j+1:]
        elif s[i+j] != t[j]: 
            break
    else: 
        for j in range(len(si)):
            if si[j] == '?':
                si =si[:j] + 'a' + si[j+1:]
        y.append(si)
y.sort()
if y == []:
    print('UNRESTORABLE')
else: print(y[0])

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

s = readline().rstrip().decode('utf-8')
t = readline().rstrip().decode('utf-8')




flag = 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] == "?":
            if j == len(t)-1:
                flag = 1
                break
        else:
            flag = 0
            break
    if flag:
        ans = ["a" for k in range(len(s))]
        
        for k in range(len(s)):
            if s[k] != "?":
                ans[k] = s[k]
        for k in range(len(t)):
            ans[i+k] = t[k]
        print("".join(ans))
        exit()

print("UNRESTORABLE")

Solution (Python):
s = input()
sl = list(s)
t = list(input())
flag = 0
for i in range(len(s) - len(t) + 1):
    cnt = 0
    for j in range(len(t)):
        if s[i + j] != t[j] and s[i + j] != '?':
            break
        else:
            cnt += 1
    if cnt == len(t):
        flag = 1
        index = i

if not flag:
    print('UNRESTORABLE')
else:
    for i in range(len(t)):
        sl[index + i] = t[i]
    for i in range(len(sl)):
        if sl[i] == '?':
            sl[i] = 'a'
    print("".join(sl))

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

for i in range(len(s)-len(t),-1,-1):
    flag=True
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?":
            flag = False
            break
    if flag:
        ans = ""
        for k in range(i):
            if s[k]!="?":
                ans+=s[k]
               
            else:
                ans += "a"
        ans+=t
        for k in range(i+len(t),len(s)):
            if s[k]!="?":
                ans+=s[k]
            else:
                ans += "a"
        print(ans)
        exit()
print("UNRESTORABLE")

Solution (Python):
import sys
import re


s = input()


t = input()

s = s.replace("?", ".")


s = s[::-1]
t = t[::-1]

match = False
for i in range(len(s)-len(t)+1):
    st = s[i:i+len(t)]
    if len(st) < len(t):
        break
    pattern = re.compile(r"%s" % st)
    if re.match(pattern, t):
        match = True
        break

if match:
    cut = i + len(t)
    return_string = s[:i] + t + s[cut:]
    return_string = return_string.replace(".", "a")
    return_string = return_string[::-1]
    print(return_string)
else:
    print("UNRESTORABLE")

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

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

possible = []
for i in range(len(s)-len(t)+1):
    temp = s[i:i+len(t)]
    flag = True
    for a, b in zip(t, temp):
        if not (a == b or b == "?"):
            flag = False
    if flag:

        possible.append((s[:i] + t + s[i+len(t):]).replace("?", "a"))

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

Solution (Python):
import fnmatch
S = input()
T = input()
 
def str_match(s, pattern):
  return fnmatch.fnmatch(pattern, s)

def build_ans(start, s, t):
  res = list(s.replace("?", "a"))
  res[start:start + len(t)] = t
  return "".join(res)

ans = []
for i in range(len(S)):
  if(i + len(T) <= len(S)):
    if(str_match(S[i:i + len(T)], T)):
      ans.append(build_ans(i,S,T))
ans.sort()
if(len(ans) > 0):
  print(ans[0])
else:
  print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
l = []
i = 0
while i < len(s) - len(t) + 1:
    for j in range(len(t)):
        if s[i+j] == "?" or s[i+j] == t[j]:
            if j == len(t) - 1:
                l.append(i)
                i += 1
            else: continue
        else:
            i += 1
            break
if not l: print("UNRESTORABLE")
else:
    idx = max(l)
    ans = list(s)
    for i in range(len(s)):
        if ans[i] == "?": ans[i] = "a"
    for i in range(len(t)):
        ans[idx+i] = t[i]
    print("".join(ans))

Solution (Python):

import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from heapq import heappop,heappush
import time
import sys
readline = sys.stdin.readline

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s

s=input()
t=input()
ans=[]
for a in range(len(s)-len(t)+1):
    temp=list(s)
    f=True
    for b in range(len(t)):
        if(temp[a+b]!="?" and t[b]!=s[a+b]):f=False
        temp[a+b]=t[b]
    if f:
        ans.append(join(temp,"").replace("?","a"))
if len(ans)==0:
    print("UNRESTORABLE")
else:
    ans.sort()
    print(ans[0])

Solution (Python):
import sys
s = input()
t = input()
ls = len(s)
lt = len(t)
noexist = 'UNRESTORABLE'
if lt>ls:
    print(noexist)
    sys.exit()
last = -1
for i in range(ls-lt+1):
    for j, c in enumerate(t):
        if s[i+j]!='?' and s[i+j]!=c:
            break
    else:
        last = i
if last < 0:
    print(noexist)
    sys.exit()
ind = 0
while(ind<ls):
    if ind==last:
        print(t, end='')
        ind += lt
    else:
        print(s[ind] if s[ind]!='?' else 'a', end='')
        ind += 1

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

ns = len(S)
nt = len(T)
idx = []

for i in range(ns - nt + 1):
    for j in range(nt):
        if S[i+j] != T[j] and S[i+j] != '?':
            break
    else:
        idx.append(i)

if idx != []:
    ans = []
    for i in idx:
        s = S[:i] + T + S[i+nt:]
        ans.append(s.replace('?', 'a'))
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

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

result = ''
for i in range(len(s)-len(t),-1,-1):
  mth = 1
  for j in range(len(t)):
    if s[i+j] != t[j]:
      if s[i+j] == '?':
        pass
      else:
        mth = 0
        break
  if mth == 1:

    result = s[:i]+t+s[i+len(t):]
    print(result.replace('?','a'))
    exit(0)

print("UNRESTORABLE")

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

s_len = len(s)
t_len = len(t)

result = ''
match = False
for i in range(s_len - t_len, -1, -1):
    match = False
    s_part = s[i:i + t_len]
    for j in range(t_len + 1):
        if j == t_len:
            result = (s[0:i] + t + s[i + t_len:]).replace('?', 'a')
            match = True
            break
        if s_part[j] == '?':
            continue
        if s_part[j] != t[j]:
            match = False
            break
    if match:
        break

print(result if match else 'UNRESTORABLE')

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 j in range(len(t)):
        if s[i+j] == '?':
            continue
        if s[i+j] != t[j]:
            break
    else:
        ans = s[:i] + t + s[i+len(t):]
        print(ans.replace('?', 'a'))
        break
else:
    print('UNRESTORABLE')

Solution (Python):
from string import ascii_lowercase

code = input()
hint = input()

def is_match(code, s):
  """
  >>> is_match('abc', 'abc')
  True
  >>> is_match('a?c', 'axc')
  True
  >>> is_match('???', 'xyz')
  True
  >>> is_match('abc', 'abd')
  False
  >>> is_match('abc', 'abcd')
  False
  >>> is_match('?p?', 'abc')
  False
  >>> is_match('', 'abc')
  False
  """
  if len(code) != len(s):
    return False
  for a, b in zip(code, s):
    if not (a == b or a == '?'):
      return False    
  return True


def guess(code, hint):
  """
  >>> guess('?tc????', 'coder')
  'atcoder'
  >>> guess('?p??d??', 'abc')
  'UNRESTORABLE'
  >>> guess('?????er', 'coder')
  'aacoder'
  >>> guess('a', 'b')
  'UNRESTORABLE'
  >>> guess('?', 'b')
  'b'
  >>> guess('abc', 'abcabc')
  'UNRESTORABLE'
  >>> guess('?????coder', 'coder')
  'aaaaacoder'
  >>> guess('?????coder?????', 'coder')
  'aaaaacoderaaaaa'
  """
  reversed_code = code[::-1] 
  reversed_hint = hint[::-1] 

  answers = []

  for i in range(len(code)):
    if is_match(reversed_code[i:i + len(hint)], reversed_hint):
      merged = reversed_code[:i] + reversed_hint + reversed_code[i + len(hint):]
      answers.append(merged[::-1].replace('?', 'a'))

  if len(answers) < 1:
    return 'UNRESTORABLE'

  answers.sort()

  return answers[0]

print(guess(code, hint))

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

def compareStr(expected: str, actual: str) -> bool:
    for e, a in zip(expected, actual):
        if e != a and a != "?":
            return False

    return True

candidates = []
for i in range(len(sd)-len(t)+1):
    if compareStr(t, sd[i:i+len(t)]):
        s = sd.replace("?", "a")
        s = s[:i] + t + s[i+len(t):]
        candidates.append(s)

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

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

l = []
for i in range(len(s)-len(t)+1):
    f = 0
    u = s[i:len(t)+i]
    for j in range(len(t)):
        if u[j] != t[j] and u[j] != "?":
            f = 1
    if f == 0:
        l.append((s[:i]+t+s[len(t)+i:]).replace("?","a"))
if l:
    print(sorted(l)[0])
else:
    print("UNRESTORABLE")

Solution (Python):
def solve(s, t):
    start = -1
    for i in range(len(s) - len(t) + 1):
        if all(s[len(s) - len(t) - i + j] in (t[j], '?') for j in range(len(t))):
            start = len(s) - len(t) - i
            break
    if start >= 0:
        return s[:start].replace('?', 'a') + t + s[start + len(t):].replace('?', 'a')
    else:
        return 'UNRESTORABLE'

_s = input()
_t = input()
print(solve(_s, _t))

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

for i in range(len(S)-len(T)+1):
    ok = True
    for j, t in enumerate(T):
        if S[i+j]!=t and S[i+j]!='?':
            ok = False
            break
    if ok: L.append(i)

S = S.replace('?', 'a')
X = []
for i in L:
    X.append(S[:i] + T + S[i+len(T):])

print(min(X) if len(X)!=0 else 'UNRESTORABLE')

Solution (Python):
S = input()
T = input()
M = len(S)
N = len(T)
for start in range(M-N,-1,-1):
    for i in range(N):
        if not (S[start+i] == '?' or S[start+i] == T[i]):
            break
    else:
        end = start + N
        pre = ''
        post = ''
        for i in range(start):
            if S[i] == '?':
                pre += 'a'
            else:
                pre += S[i]
        for i in range(end,M):
            if S[i] == '?':
                post += 'a'
            else:
                post += S[i]
        print(pre + T + post)
        exit()
else:
    print('UNRESTORABLE')

Solution (Python):


s, t = input(), input()
for i in range(len(s) - len(t), -1, -1):
    tt = s[i : i + len(t)]
    flag = True
    for j in range(len(t)):
        if tt[j] == t[j] or tt[j] == "?":
            continue
        else:
            flag = False
    if flag:
        ans = list(s[:i] + t + s[i + len(t) :])
        for k in range(len(s)):
            if ans[k] == "?":
                ans[k] = "a"
        print("".join(ans))
        exit()
print("UNRESTORABLE")

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

if len(S) < len(T):
    print('UNRESTORABLE')
else:
    pos = []
    
    for i in range(len(S)-len(T)+1):
        
        match = True
        for j in range(len(T)):
            
            if T[j] == S[i+j] or S[i+j] == '?':
                pass
            else:
                match = False
        if match:
            pos.append(i)

    
    if len(pos) == 0:
        print('UNRESTORABLE')
    else:
        ans = 'z' * 51
        for pi in pos:
            r = ''
            for j in range(pi):
                if S[j] == '?':
                    r += 'a'
                else:
                    r += S[j]
            r += T
            for j in range(pi+len(T), len(S)):
                if S[j] == '?':
                    r += 'a'
                else:
                    r += S[j]
            ans = min(ans, r)
        print(ans)

Solution (Python):
s = input()
t = input()
ss = len(s)
l = len(t)
ans = []
tl = list(t)
for i in range(len(s) - l + 1):
    tl2 = list(s[i:i + l])
    f=1
    for j in range(l):
        if tl[j] != tl2[j] and tl2[j] != "?":
            f=0
            break
    if f==1:
        s1=s[:i].replace("?","a")
        s2=s[i+l:].replace("?","a")
        ans.append(s1 + t + s2)
if len(ans)==0:
    print("UNRESTORABLE")
else:
    ans.sort()
    print(ans[0])

Solution (Python):

S=list(input())
T=list(input())
flg=False
S.reverse()
T.reverse()

for i in range(max(0,len(S)-len(T)+1)):
    flg=True
    for j in range(len(T)):
        if not(S[i+j]=="?"):
            if S[i+j]!=T[j]:
                flg=False
    if flg:
        for k in range(len(T)):
            S[i+k]=T[k]
        break    
        

S.reverse()

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

Solution (Python):
S = input()
T = input()
flag = 0
anslist = []
for i in range(len(S)-len(T)+1):
    flag = 1
    for j in range(len(T)):
        if not (S[i+j] == T[j] or S[i+j] == "?"):
            flag = 0
            break
    if flag == 1:
        anslist.append(i)

anslist.sort(reverse=True)
flag = 0
count2 = 0
if len(anslist) == 0:
    print("UNRESTORABLE")
else:
    for i in range(len(S)):
        if i == anslist[0]:
            flag = 1
            count2 = 0
        if flag == 1:
            print(T[count2], end="")
            count2+=1
            if count2 == len(T):
                flag = 0
                continue
        if flag == 0:

            if S[i] == "?":
                print("a", end="")
            else:
                print(S[i], end="")

Solution (Python):
sd = input()
T = input()
answer = []
if len(sd) < len(T):
    print('UNRESTORABLE')
    exit(0)
for i in range(len(sd)-len(T)+1):
    s = sd
    for j in range(len(T)):
        if sd[i+j] == T[j] or sd[i+j] == '?':
           s = s[:i+j] + T[j] + s[i+j+1:]
        else:
            break 
    else:
        answer.append(s.replace('?', 'a'))
if len(answer) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(answer)[0])

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

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

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

c = -1
for i in range(ls-lt+1):
    if match(i):
        c = i

if c == -1:
    print('UNRESTORABLE')
else:
    ans = s[:c]
    ans += t
    ans += s[len(ans):]
    print(ans.replace('?', 'a'))

Solution (Python):




import sys

s = list(input())
sl = len(s)
t = list(input())
tl = len(t)

for i in range(sl-tl,-1,-1):
    count = 0
    for j in range(tl):
        if s[i+j] == "?" or s[i+j] == t[j]:
            count +=1
    if count == tl:
        ans = (''.join((s[0:i] + t + s[i+tl:]))).replace("?","a")
        print(ans)
        sys.exit()

print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
lenS = len(S)
lenT = len(T)

for i in range(lenS - lenT, -1, -1):
    flag = True
    for ii in range(lenT):
        if S[i+ii] == '?' or S[i+ii] == T[ii]:
            continue
        else:
            flag = False
            break
    if flag == True:
        print((S[:i] + T + S[i + lenT:]).replace('?', 'a'))
        exit()
print('UNRESTORABLE')

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

if __name__ == '__main__':
    main()

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

ans = []
for i in range(len(sdush)-len(t)+1):
  for index,c in enumerate(t):
    if c != sdush[i+index] and sdush[i+index] != '?':
      break
    if index+1 == len(t):
      ans.append(sdush[:i]+t+sdush[i+1+index:])

result = "z"*50
for an in ans:
  if result > an:
    result = an
if result == "z"*50:
  print("UNRESTORABLE")
else:
  print(result.replace("?","a"))

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    S = input().strip()
    T = input().strip()
    ans = list(S)

    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:
            m = 0
            for k in range(i, i + len(T)):
                ans[k] = T[m]
                m += 1
            for k in range(len(ans)):
                if ans[k] == "?":
                    ans[k] = "a"
            print("".join(ans))
            exit()
    else:
        print("UNRESTORABLE")


if __name__ == '__main__':
    main()