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):
def solve():
    S = list(input())
    ans = S[:]
    T = input()
    Sn = len(S)
    Tn = len(T)
    
    restorable = False
    for i in range(Sn - Tn, -1, -1):
        flag = True
        if S[i] == T[0] or S[i] == '?':
            for j in range(Tn):
                if S[i+j] != T[j] and S[i+j] != '?':
                    flag = False
                    break
            if flag:
                restorable = True
                for j in range(Tn):
                    ans[i+j] = T[j]
                break
    
    if not restorable:
        print('UNRESTORABLE')
    else:
        for i in range(Sn):
            if ans[i] == '?':
                ans[i] = 'a'
        print(''.join(ans))


if __name__ == '__main__':
    solve()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

def check(S, T):
    N = len(S)
    for i in range(N):
        if S[i] != '?' and S[i] != T[i]:
            return False
    return True


ans = [''] * N
for i in range(N-M, -1, -1):
    
    if check(S[i:i+M], T):
        for j in range(i, i+M):
            ans[j] = T[j-i]
        break
else:
    
    print('UNRESTORABLE')
    exit()

for i in range(N):
    if S[i] != '?':
        ans[i] = S[i]
    
    elif ans[i] == '':
        ans[i] = 'a'
print(''.join(ans))

Solution (Python):

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd



def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)



show_flg = True


def main():
    S = input()
    T = input()
    canditates = []

    for i in range(len(S)):
        idx = 0
        for j in range(i, len(S)):
            if idx < len(T) and (S[j] == T[idx] or S[j] == '?'):
                idx += 1
            else:
                break

        
        if idx == len(T):
            canditates.append(S[:i] + T + S[i+len(T):])
            
            

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = input()
    t = input()
    T = len(t)
    candidate = []
    for i in range(len(s)-T+1):
        buf = s[i:i+T]
        flag = True
        for b, tc in zip(buf, t):
            if b == "?" or b == tc:
                continue
            else:
                flag = False
                break
        if flag == True:
            candidate.append(i)
    if len(candidate) == 0:
        print("UNRESTORABLE")
    else:
        print((s[:candidate[-1]] + t + s[candidate[-1]+T:]).replace("?", "a"))
if __name__ == '__main__':
    main()

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

ans = "UNRESTORABLE"

i = 0

while i < len(s)-len(t)+1:
    if s[i] == t[0] or s[i] == "?":
        flag = True
        for j in range(1,len(t)):
            if s[i+j] == "?" or s[i+j] == t[j]:
                continue
            else:
                flag = False
                break
        if flag:
            tmps = ""
            k = 0
            while k < len(s):
                if k < i or k >= i+len(t):
                    if s[k] == "?":
                        tmps += "a"
                        k += 1
                    else:
                        tmps += s[k]
                        k += 1
                else:
                    tmps += t
                    k += len(t)
            ans = tmps
    i += 1

print(ans)

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

def compaierStr(expected: list, actual: list) -> 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 compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

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

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

def compaierStr(expected: list, actual: list) -> 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 compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

if len(candidates) == 0:
    print("UNRESTORABLE")
else:
    ans = "z"*(len(sd))
    for _c in candidates:
        if _c < ans:
            ans = _c

    print(ans)

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

def compaierStr(expected: list, actual: list) -> 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 compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

if len(candidates) == 0:
    print("UNRESTORABLE")
else:
    ans = "z"
    for _c in candidates:
        if _c < ans:
            ans = _c

    print(ans)

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):
sd = str(input())
t = str(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):
import sys

s_prime, t = sys.stdin.read().split()
ls = len(s_prime)
lt = len(t)

def main():
    for i in range(ls-lt, -1, -1):
        t_prime = s_prime[i:i+lt]
        for j in range(lt):
            if not t_prime[j] in ['?', t[j]]:
                break
        else:
            s = (s_prime[:i] + t + s_prime[i+lt:]).replace('?', 'a')
            return s
    return 'UNRESTORABLE'
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
import sys

s_prime, t = sys.stdin.read().split()
s = list(s_prime)
ls = len(s)
lt = len(t)

def main():
    ng = 'UNRESTORABLE'

    if ls < lt:
        return ng

    for i in range(ls-lt, -1, -1):
        t_prime = s[i:i+lt]
        for j in range(lt):
            letter = t_prime[j]
            if (letter != '?') and (letter != t[j]):
                break
        else:
            for j in range(lt):
                s[i+j] = t[j]
            break
    else:
        return ng
    
    ans = ''.join(s).replace('?', 'a')
    return ans
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
s,t = input(), input()
ls,lt = len(s), len(t)
for i in range(ls-lt,-1,-1):
  for j,s_ in enumerate(s[i:i+lt]):

    if (s_ == '?') | (s_ == t[j]):
      pass
    else:
      break
  else:
    s = s[:i] + t + s[i+lt:]
    s = s.replace('?','a')
    print(s)
    break
else:
  print('UNRESTORABLE')

Solution (Python):
S=input();T=input();
M=-1;
for i in range(len(S)-len(T)+1):
  t=True;
  for j in range(len(T)):
    t*=(S[i+j]==T[j] or S[i+j]=='?');
  if(t):
    M=i;
if(M==-1):
  print("UNRESTORABLE");
  exit();
print((S[:M]+T+S[M+len(T):]).replace("?","a"));

Solution (Python):
s=input()
t=input()
ls=len(s)
lt=len(t)
if ls<lt:
    print('UNRESTORABLE')
    exit()
n=ls-lt+1
ans=[]
for i in range(n):
    tt='?'*i+t+'?'*(n-i-1)
    a=''
    for j in range(ls):
        if tt[j]!='?':
            if tt[j]!=s[j] and s[j]!='?':
                break
            a+=tt[j]
        else:
            if s[j]=='?':
                a+='a'
            else:
                a+=s[j]
    else:
        ans.append(a)
if ans:
    ans=sorted(ans)
    print(ans[0])
else:
    print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()
s, t = len(S), len(T)
p = 'UNRESTORABLE'
n = -1
if t <= s:
  for i in range(s-t+1):
    f = 1
    for j in range(t):
      if S[i+j] != '?' and S[i+j] != T[j]:
        f = -1
    if f == 1:
      n = i
  if n >= 0:
    p = ''
    for k in range(s):
      if k < n or k > n + t - 1:
        if S[k] == '?':
          p = p[:] + 'a'
        else:
          p = p[:] + S[k]
      else:
        p = p[:] + T[k - n]
print(p)

Solution (Python):
s = input()
t = input()
result = ''
length = len(s)-len(t)
while length>=0:
  skip = False
  for i in range(len(t)):
    if s[length+i]!='?' and s[length+i]!=t[i]:
      skip = True
      break
  if skip==False:
    break
  length-=1
    
if length<0:
  print("UNRESTORABLE")
else:
  i = 0
  while i<len(s):
    if i==length:
      result+=t
      i+=len(t)-1
    else:
      if s[i]=='?':
        result+='a'
      else:
        result+=s[i]
    i+=1
  print(result)

Solution (Python):
s=list(input())
t=list(input())
ls=len(s)
lt=len(t)
if ls<lt:
  print("UNRESTORABLE")
  exit()
for i in range(ls-lt,-1,-1):
  for j in range(i,i+lt):
    if s[j]==t[j-i] or s[j]=="?":
      continue
    else:
      break
  else:
    for k in range(i,i+lt):
      s[k]=t[k-i]
    for l in range(ls):
      if s[l]=="?":
        s[l]="a"
    print(*s,sep="")
    exit()
else:
  print("UNRESTORABLE")

Solution (Python):
def getLastMatch(s,t):
    for i in range(len(s)-len(t), -1, -1):
        if s[i] == '?' or s[i] == t[0]:
            match = True
            for j in range(len(t)):
                if s[i+j] == '?': continue
                if s[i+j] != t[j]:
                    match = False
                    break
            if match: return i
    return -1

def getS(s,t,lastMatch):
    s = list(s)
    for i in range(len(t)):
        s[i+lastMatch] = t[i]
    for i in range(len(s)):
        if s[i]=='?': s[i] = 'a'
    res = ''
    for c in s: res+=c
    return res


s,t = input(), input()
lastMatch = getLastMatch(s,t)
if lastMatch == -1:
    print('UNRESTORABLE')
else:
    print(getS(s,t,lastMatch))

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

len_s = len(S)
len_t = len(T)

match = -1

for i in range((len_s - len_t), -1, -1):
    for j in range(len_t):
        if T[j] != S[i+j] and S[i+j] != "?":
            break

        if j == len_t-1:
            match = i

    if match != -1:
        break

if match == -1:
    print("UNRESTORABLE")
    exit()

list_s = list(S)

for i in range(match, match+len_t):
    list_s[i] = T[i - match]

list_s = [x if x != "?" else "a" for x in list_s]

print("".join(list_s))

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

now = 0

flag2 = False

for i in reversed(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 == True:
        flag2 = True
        for j in range(len(T)):
            S_[i + j] = T[j]
        break

S = ''
for i in S_:
    if i == '?':
        S += 'a'
    else:
        S += i

if flag2 == True:
    print(S)
else:
    print('UNRESTORABLE')

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

n = len(sd)
m = len(t)
s = []

for i in range(n - m, -1, -1):
    t_kamo = sd[i:i + m]

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

print("UNRESTORABLE")

Solution (Python):
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()
for i in range(len(S)-len(T)+1)[::-1]:
    for j in range(len(T))[::-1]:
        if S[i+j] != T[j] and S[i+j] != "?":
            break
    else:
        S = S[:i] + T + S[i+len(T):]
        print(S.replace("?", "a"))
        exit()
print("UNRESTORABLE")

Solution (Python):
s_dash = input()
t = input()
match_index = []
candidate = []
for i in range(len(s_dash) - len(t) + 1):
    for j in range(len(t)):
        if not(t[j] == s_dash[i + j] or s_dash[i + j] == '?'):
            break
        if j == len(t) - 1:
            match_index.append(i)
for index in match_index:
    s = s_dash[:index] + t
    if index != len(s_dash) - len(t):
        s += s_dash[index + len(t):]
    s = list(s)
    for i in range(len(s)):
        if s[i] == '?':
            s[i] = 'a'
    s = "".join(s)
    candidate.append(s)
candidate.sort()
if len(candidate) == 0:
    print('UNRESTORABLE')
else:
    print(candidate[0])

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

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

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    lt = len(t)
    ans = []
    for i in range(len(s) - len(t), -1, -1):
        ts = s[i:i+lt]
        b = True
        for j in range(lt):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            for j in range(len(s)):
                if i <= j < i + lt:
                    ans.append(t[j-i])
                else:
                    ans.append(s[j] if s[j] != "?" else "a")
            print("".join(ans))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Solution (Python):
S = input()
T = input()
M = len(S)
N = len(T)

def strmap(s, c_from='?', c_to='a'):
    return "".join(map(lambda c: c_to if c == c_from else c, s))

keys = []
for first in range(M - N + 1):
    match = True
    for i in range(N):
        if S[first + i] != '?' and S[first + i] != T[i]:
            match = False
            break
    if match:
        keys.append(strmap(S[: first]) + T + strmap(S[first + N:]))

if keys:
    print(min(keys))
else:
    print("UNRESTORABLE")

Solution (Python):
from copy import deepcopy

def judge_a(S,T,i,j):
    return S[i+j]==T[j]

def judge_b(S,T,i,j):
    return S[i+j]!=T[j] and S[i+j]=="?"

def ans(ans_list):
    if not ans_list:
        print("UNRESTORABLE")
    else:
        ans_list_2=[]
        for l in ans_list:
            temp=[s if s!="?" else "a" for s in l]
            ans_list_2.append("".join(temp))
        print(min(ans_list_2))

if __name__ == '__main__':
    S_dash=list(input())
    T=list(input())
    N_S=len(S_dash)
    T_N=len(T)
    ans_list=[]

    for i in range(N_S-T_N+1):
        S_2=deepcopy(S_dash)
        flag=True
        for j in range(T_N):
            if judge_a(S_2,T,i,j) or judge_b(S_2,T,i,j):
                S_2[i+j]=T[j]
            else:
                flag=False
                break
        if flag:
            ans_list.append(S_2)
    ans(ans_list)

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

ans = "UNRESTORABLE"

ls = []

for i in range(len(t)):
    for j in range(i, len(s)-len(t)+i+1):
        if s[j] == t[i]:
            for k in range(len(t)):
                if t[k] != s[j+k-i] and s[j+k-i] != "?":
                    break
                if k == len(t)-1:
                    tmps = s[:j-i] + t + s[j-i+len(t):]
                    for p in range(len(s)):
                        if tmps[p] == "?":
                            tmps = tmps[:p] + "a" + tmps[p+1:]
                    ls.append(tmps)

for i in range(len(s)-len(t)+1):
    if s[i] == "?":
        if s[i:i+len(t)] == "?"*len(t):
            tmps = s[:i] + t + s[i+len(t):]
            for p in range(len(s)):
                if tmps[p] == "?":
                    tmps = tmps[:p] + "a" + tmps[p+1:]
            ls.append(tmps)

ls.sort()
if len(ls) != 0:
    ans = ls[0]

print(ans)

Solution (Python):
import re

s = str(input())
t = str(input())










NS = []

for i in range(len(s)):
    
    s_res = s[:i]
    s_res = s_res.replace('?', 'a')
    
    
    
    s_obj = s[i:i+len(t)]
    s_obj = s_obj.replace('?', '.')
    s_comp = re.compile(s_obj)

    s_res_f = s[i+len(t):]
 
    matchobj = s_comp.fullmatch(t)

    if matchobj:
        start = matchobj.start()
        new_s = s_res + t + s_res_f
        new_s = new_s.replace('?', 'a')
        NS.append(new_s)

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

print(ans)

Solution (Python):


S=input()
T=input()

def match(S,T):
    for i in range(len(T)):
        if S[i]==T[i] or S[i]=='?':
            continue
        else:
            return False
    return True

if len(T)>len(S):
    print('UNRESTORABLE')
    exit()

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

Solution (Python):
s = input()
t = input()
S = len(s)
T = len(t)

ans = []
for i in range(S - T + 1):
    tmp = list(s)
    cnt = 0
    for j in range(T):
        if s[i + j] == t[j] or s[i + j] == "?":
            cnt += 1
            tmp[i + j] = t[j]
        if cnt == T:
            ans.append("".join(tmp).replace("?", "a"))

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

Solution (Python):
s = input()
t = input()
S = len(s)
T = len(t)

ans = []
for i in range(S):
    if S - i >= T:
        tmp = list(s)
        cnt = 0
        for j in range(T):
            if s[i + j] == t[j] or s[i + j] == "?":
                cnt += 1
                tmp[i + j] = t[j]
            if cnt == T:
                ans.append("".join(tmp).replace("?", "a"))

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

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

len_S = len(S)
len_T = len(T)

if len_S < len_T:
    print("UNRESTORABLE")
    quit()

candidates = tuple((S[:i] + T + S[i + len_T:]).replace("?", "a") for i in range(len_S - len_T + 1) if all(s in ("?", t) for s, t in zip(S[i:], T)))

print(min(candidates) if candidates else "UNRESTORABLE")

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

last = -1
for i in range(len(s) - len(t)+1):
    for a,b in zip(s[i:i+len(t)], t):
        if a == "?":
            continue
        if a != b:
            break
    else:
        last = i

if last == -1:
    print("UNRESTORABLE")
    import sys
    sys.exit()


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

for i in range(len(t)):
    ans[i+last] = t[i]

print("".join(ans))

Solution (Python):
ss, t = input(), input()
lt = len(t)
lss = len(ss)
lst = []
for i in range(lss - lt + 1):
    tss = ss[i:i + lt]
    tb = True
    for a, b in zip(tss, t):
        if a == '?':
            continue
        elif a != b:
            tb = False
            break
    if tb:
        lst.append(ss[:i] + t + ss[i + lt:])
if len(lst):
    lst.sort()
    print(lst[0].replace('?', 'a'))
else:
    print('UNRESTORABLE')

Solution (Python):
ss, t = input(), input()
lt = len(t)
lss = len(ss)
lst = []
for i in range(lss - 1, lt - 2, -1):
    tss = ss[i - lt + 1:i + 1]
    tb = True
    for a, b in zip(tss, t):
        if a == '?':
            continue
        elif a != b:
            tb = False
            break
    if tb:
        lst.append(ss[:i - lt + 1] + t + ss[i + 1:])
if len(lst):
    lst.sort()
    print(lst[0].replace('?', 'a'))
else:
    print('UNRESTORABLE')

Solution (Python):
s=input()
n=len(s)
t=input()
m=len(t)

ANS=[]
for i in range(n-m+1):
    for j in range(m):
        
        if s[i+j] != "?" and s[i+j] != t[j]:
            break
    else:
        ans = list(s)
        ans[i:i+j+1] = list(t)
        ans="".join(ans)
        ans = ans.replace('?','a')
        ANS.append(ans)
if len(ANS) == 0:
    print("UNRESTORABLE")
else:
    ANS.sort()
    print(ANS[0])

Solution (Python):
import re


S_ = input()
T = input()

S_ = S_[::-1]
T = T[::-1]

t = ''
for c in T:
    t += '[?' + c + ']'
regex = re.compile(t)

S, cnt = regex.subn(T, S_, count=1)
if cnt:
    S = S.replace('?', 'a')
    print(S[::-1])
else:
    print('UNRESTORABLE')

Solution (Python):
import sys
s = input()[::-1]
t = input()[::-1]
ans = 'UNRESTORABLE'
if len(t)>len(s):
  print(ans)
  sys.exit()
for i in range(len(s)-len(t)+1):
  flag = True
  for j in range(len(t)):
    if t[j] != s[i+j] and s[i+j] != '?':
      flag = False
  if flag:
    for j in range(len(t)):
      ans = s[:i]+t+s[i+len(t):]
      ans = ans.replace('?','a')
      ans = ans[::-1]
      print(ans)
      sys.exit()
print(ans)

Solution (Python):
S = list(input())
lenS = len(S)
T = list(input())
lenT = len(T)
Flag = False
for i in range(lenS - lenT, -1 , -1):
    for j in range(lenT - 1, -1 , -1):
        if S[i + j] != "?" and T[j] != S[i + j]:
            break
    else:
        Flag = True
        for j in range(lenT - 1 , -1 , -1):
            S[i + j] = T[j]
        break
if Flag:
    for i in range(lenS):
        if S[i] == "?":
            S[i] = "a"
    print("".join(S))
else:
    print("UNRESTORABLE")

Solution (Python):
S = input()
SA  = S.replace('?','a')
T = input()

def test(i):
    
    S_sub = S[i:i+len(T)]
    for s,t in zip(S_sub,T):
        if s == '?':
            continue
        if s != t:
            return False
    return True

pw = []
for i in range(len(S)-len(T)+1):
    if test(i):
        x = SA[:i] + T + SA[i+len(T):]
        pw.append(x)
if pw:
    answer = sorted(pw)[0]
else:
    answer = 'UNRESTORABLE'
print(answer)

Solution (Python):
def ABC076C_DubiousDocument2():
    Sd = list(input().strip())
    T = list(input().strip())

    tlen = len(T)
    slen = len(Sd)

    ans = 'UNRESTORABLE'
    nowPos = 0
    for pos in range(slen - 1, -1, -1):
        if pos < (tlen - 1):
            break
        if(T[tlen - 1] == Sd[pos]) or (Sd[pos] == '?'):
            nowPos = pos
            match = True
            for chk in range(nowPos, nowPos - tlen, -1):
                if(T[tlen - (nowPos - chk) - 1] != Sd[chk]) and (Sd[chk] != '?'):
                    match = False
                    break

            if match:
                
                for chk in range(nowPos, nowPos - tlen, -1):
                    Sd[chk] = T[tlen - (nowPos - chk) - 1]
                for i in range(0, slen):
                    if Sd[i] == '?':
                        Sd[i] = 'a'
                
                ans = ''.join(Sd)

    print(ans)

def main():
    ABC076C_DubiousDocument2()

if __name__ == "__main__":
    main()

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


def match(x, y):
   
    return all(x[i] == y[i] or y[i] == '?' for i in range(N))


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

Solution (Python):
S = input()
T = input()
s_l = len(S)
t_l = len(T)
t=list(T)
ans = 'UNRESTORABLE'
for i in range(s_l-t_l+1):
  s = list(S[i:i+t_l])
  count=0
  for a,b in zip(s,t):
    if (a==b)or(a=='?'):
      count+=1
      if count==t_l:
        ans = S[:i]+T+S[i+t_l:]
        
    else:break
        
print(ans.replace('?','a'))

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

    rs = S.replace("?","a")

    candidate = None

    for i in range(len(S)-len(T)+1):
        ts = S[i:i+len(T)]
        
        is_mismatched = False
        for ii in range(len(T)):
            if ts[ii] != "?" and ts[ii] != T[ii]:
                is_mismatched = True
                break
        if not is_mismatched:
            candidate = rs[:i] + T + rs[i+len(T):]
           
    return candidate if candidate else "UNRESTORABLE"

if __name__ == '__main__':
    print(main())

Solution (Python):

S = input()
T = input()

ans = "UNRESTORABLE"
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
        else:
            break

        if cnt == len(T):
            
            ans = S[:i] + T + S[i+len(T):]
            ans = ans.replace("?", "a")
            
            
        
print(ans)

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

a = []
for i in range(ls-lt+1):

    f=True
    sa = list(s.replace('?', 'a'))
    for j in range(lt):
        sa[i+j] = t[j]
        if s[i+j]!='?' and s[i+j]!=t[j]:
            f=False
            break
    if f:
        a.append(''.join(sa))

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

Solution (Python):
S = input()
T = input()
count = 0
flg = False
if len(S) < len(T):
    print('UNRESTORABLE')
    exit()
while True:
    flg = False
    for i in range(1,len(T)+1):
        if S[-i-count] != T[-i] and S[-i-count] != '?':
            count += 1
            flg = True
            break
    if count == len(S) - len(T) + 1:
        print('UNRESTORABLE')
        exit()
    if not flg:
        if count == 0:
            ans = S[0:len(S)-len(T)] + T
        elif count == len(S)-len(T):
            ans = T + S[len(T):]
        else:
            ans = S[0:len(S)-len(T)-count] + T + S[-count:]
        print(ans.replace('?','a'))
        exit()

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

ANS = False
save_left = 0
save_right = 0
for i in range(len(S) - len(T) + 1):
    count = 0
    left = i
    right = i + len(T)

    for sv, tv in zip(S[left:right], T):
        if sv != tv and sv != '?':
            break
        count += 1

    if count == len(T):
        save_left = left
        save_right = right
        ANS = True
     
if ANS:
    S[save_left:save_right] = T
    for w in S:
        if w == '?':
            print('a', end='')
        else: 
            print(w, end='')
else:
    print('UNRESTORABLE')

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

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

print(min(str_list).replace("?","a"))

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+m:]).replace("?","a"))
            exit()
        if t_kamo[j]=="?":
            continue
        elif t_kamo[j]!=t[j]:
            break
print("UNRESTORABLE")

Solution (Python):

S = input()
T = input()
check = -1
for i in range(len(S) - len(T) + 1):
    if S[i] == "?" or S[i] == T[0]:
        k = 1
        flag = True
        while k < len(T):
            if S[i+k] == "?":
                k += 1
            else:
                if S[i + k] == T[k]:
                    k += 1
                else:
                    flag = False
                    break
        if k == len(T) and flag == True:
            check = i

if check != -1:
    S = list(S)
    for i in range(len(T)):
        S[check + i] = T[i]
    S = "".join(S)
    ans = S.replace("?", "a")
    print(ans)
else:
    print("UNRESTORABLE")

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

Sa = []

if m >= n:
    for i in range(m+1-n):
        for j in range(n):
            if S[i+j] != T[j] and S[i+j] != "?":
                break
        else:
            s = S[:i] + T + S[i+n:]
            Sa.append(s.replace("?", "a"))
            

if Sa:
    print(min(Sa))
else:
    print("UNRESTORABLE")

Solution (Python):
from collections import deque
s = input()
t = input()
ans = 'UNRESTORABLE'

def match_str(s, t):
    for i, c in enumerate(s):
        if t[i] != c and c != '?':
            return False
    return True

t_len = len(t)
end = len(s) - len(t) +1


for i in reversed(range(end)):

    if match_str(s[i : i+t_len], t):
        ans = s[:i].replace('?', 'a') + t +s[i+t_len:].replace('?', 'a')
        break
        
print(ans)