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

Solution (Python):
S = input()
T = input()
res = "UNRESTORABLE"

for i in range(len(S)-1, len(T)-2, -1):

  s = list(S[::])
  j = i
  flag = True
  for k in range(len(T)-1, -1, -1):
    if s[j] == "?":
      s[j] = T[k]
      j -= 1
    elif s[j] == T[k]:
      j -= 1
      continue
    elif s[j] != T[k]:
      flag = False
      break

  if flag:
    for j in range(len(s)):
      if s[j] == "?": s[j] = "a"
    res = "".join(s)
    break

print(res)

Solution (Python):


S = input()
T = input()

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

ans = []

for i in range(len(S) - len(T) + 1):
    idx = []
    for m, j in enumerate(range(i, i + len(T))):
        idx.append(j)
        if S[j] != "?" and S[j] != T[m]:
            break
    else:
        idx = set(idx)
        tmp = [s for s in S]
        now = 0
        for k in range(len(S)):
            if tmp[k] == "?":
                if k not in idx:
                    tmp[k] = "a"
                else:
                    tmp[k] = T[now]
                    now += 1
            else:
                if k in idx:
                    now += 1
        ans.append("".join(tmp))

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

Solution (Python):
ss = input()
t = input()
sl = len(ss)
tl = len(t)
min = "UNRESTORABLE"

for i in range(sl-tl+1):
    for j, tj in enumerate(t):
        if not(ss[i+j] == tj or ss[i+j] == "?"):
            break
        if j == tl-1:
            s = ""
            for k, sk in enumerate(ss):
                if i <= k and k < i+tl:
                    s += t[k-i]
                elif sk == "?":
                    s += "a"
                else:
                    s += sk
            if min == "UNRESTORABLE":
                min = s
            elif min > s:
                min = s

print(min)

Solution (Python):



s=input()
t=input()
b=[]

if len(s)-len(t)<0:
    flg=False
else:
    for i in range(len(s)-len(t)+1):
        ans=s
        for j in range(len(t)):
            flg=True
            if s[i+j]=='?' or s[i+j]==t[j]:
                
                ans=ans[:i+j]+t[j]+('' if i+j+1>len(s) else ans[i+j+1:])
            else:
                flg=False
                break
        if flg==True:
            b.append(ans.replace('?','a'))

b.sort()

print(b[0] if len(b)>0 else 'UNRESTORABLE')

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = readline().rstrip().decode()
t = read().rstrip().decode()
ans = []
for i in range(len(s) - len(t) + 1):
    for j, (ss, tt) in enumerate(zip(s[i:], t)):
        if ss not in ('?', tt):
            break
    else:
        memo = s[:i] + t + s[i + len(t):]
        ans.append(memo.replace('?', 'a'))
print(min(ans) if ans else 'UNRESTORABLE')

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

if len(S)<len(T):
    print("UNRESTORABLE")
    
else:
    for i in range(N-M+1):
        cond=True
        temp=S[N-M-i:N-i]
        for s in range(M):
            if temp[s]!="?" and temp[s]!=T[s]:
                cond=False
        if cond:
            SL[N-M-i:N-i]=list(T)
            break
    if not cond:
        print("UNRESTORABLE")
    else:
        for j in range(N):
            if SL[j]=="?":
                SL[j]="a"
    
        for a in SL:
            print(a,end="")

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

    lenS = len(S); lenT = len(T)
    idx = -1
    for i in range(lenS-lenT+1):
        if S[i] in ['?', T[0]]:
            flag = True
            for j in range(1, lenT):
                if not S[i+j] in ['?', T[j]]:
                    flag = False
                    break
            if flag:
                idx = max(idx, i)
    if idx == -1:
        print('UNRESTORABLE')
        return
    
    s = S[:idx] + T + S[idx+lenT:]
    ans = ''.join(s).replace('?', 'a')
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
t = input()
ans = []
for i in range(len(s)-len(t)+1):
    tmp = s[i:i+len(t)]
    flg = True
    for j in range(len(t)):
        if tmp[j]!=t[j]:
            if tmp[j]!='?':
                flg = False
    if flg:
        c = s[:i]+t+s[i+len(t):]
        ans.append(c.replace('?', 'a'))
print('UNRESTORABLE' if len(ans)==0 else min(ans))

Solution (Python):
import sys
S = list(input())
T = list(input())
l = len(S)
k = len(T)

if k > l:
    print("UNRESTORABLE")
else:
    for i in range(l-k,-1,-1):
        p = 0
        for j in range(k):
            if S[i+j] != T[j] and S[i+j] != "?":
                p = 1
        if p == 0:
            for st in range(i):
                if S[st] == "?":
                    print("a",end="")
                else:
                    print(S[st],end="")
            print("".join(T),end="")
            for st in range(i+k,l):
                if S[st] == "?":
                    print("a", end="")
                else:
                    print(S[st], end="")
            sys.exit()
    print("UNRESTORABLE")

Solution (Python):
import re
S2=input().strip().replace("?",".")
T=input()
ans="UNRESTORABLE"
if len(S2)<len(T):
    pass
else:
    for i in range(len(S2)-len(T)+1):
        m=re.match(S2[len(S2)-len(T)-i:len(S2)-i],T)
        if m != None:
            ans=S2[:len(S2)-len(T)-i]+ T + S2[len(S2)-i:]
            ans=ans.replace(".","a")
            break

print(ans)

Solution (Python):
import sys
import re
sys.setrecursionlimit(1000000) 

def main():
    input = sys.stdin.readline  
    S = input().rstrip()
    T = input().rstrip()

    word_size_s = len(S)
    word_size_t = len(T)
    for i in range(word_size_s - word_size_t, -1, -1):
        part_s = S[i:i+word_size_t]
        part_s = part_s.replace("?", ".")
        m = re.match(part_s, T)
        if m:
            S = (S[:i] + T + S[i+word_size_t:]).replace("?", "a")
            print(S)
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    main()

Solution (Python):



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

sr = ''.join(list(reversed(s)))
tr = ''.join(list(reversed(t)))

slen = len(sr)
tlen = len(tr)

for i in range(slen-tlen+1):
    flg = i
    for j in range(tlen):
        if sr[i+j] == tr[j] or sr[i+j] == "?":
            continue
        else:
            flg = -1
            break
    else:
        
        
        break
else:
    flg = -1
    print("UNRESTORABLE")
    exit()



before_result_reversed = sr[:flg] + tr + sr[flg+tlen:]
before_result = ''.join(list(reversed(before_result_reversed)))


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

Solution (Python):



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

sr = ''.join(list(reversed(s)))
tr = ''.join(list(reversed(t)))

slen = len(sr)
tlen = len(tr)

for i in range(slen-tlen+1):
    flg = i
    for j in range(tlen):
        if sr[i+j] == tr[j] or sr[i+j] == "?":
            continue
        else:
            flg = -1
            break
    else:
        
        
        break
else:
    flg = -1
    print("UNRESTORABLE")
    exit()



before_result_reversed = sr[:flg] + tr + sr[flg+tlen:]
before_result = ''.join(list(reversed(before_result_reversed)))


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

Solution (Python):
S = input()
T = input()
 
N = len(S)
M = len(T)
 
ans = 'UNRESTORABLE'
for i in range(N-M,-1,-1):
    b = True
    for j in range(M):
        if S[i+j] == '?' or S[i+j] == T[j]:
            continue
        b = False
    if b:
        ans = S[:i] + T + S[i+j+1:]
        break
ans = list(ans)
for i in range(len(ans)):
    if ans[i] == '?':
        ans[i] = 'a'
print(''.join(ans))

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

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

if N < M:
    print('UNRESTORABLE')
    
else:

    ans = 'UNRESTORABLE'
    for i in range(N-M+1):
        make = True
        for j in range(M):
            if S[i+j] != '?' and S[i+j] != T[j]:
                make = 0
        if make:
            ans = S[:i] + T + S[i+M:]
    if ans == 'UNRESTORABLE':
        print(ans)
    else:
        ans = list(ans)
        for i in range(N):
            if ans[i] == '?':
                ans[i] = 'a'
        print(''.join(ans))

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

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

if N < M:
    print('UNRESTORABLE')
    exit()

ans = 'UNRESTORABLE'
for i in range(N-M+1):
    anss = S[:i] + T + S[i+M:]
    make = 1
    anss = list(anss)
    for j in range(N):
        if anss[j] == '?':
            if S[j] == '?':
                anss[j] = 'a'
            else:
                anss[j] = S[j]
        else:
            if anss[j] != S[j] and S[j] != '?':
                make = 0
                break
    if make:
        ans = ''.join(anss)
print(ans)

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

def rplc(s, i, T):
  s = s[:i] + T + s[i+len(T):]
  return ''.join(s).replace('?', 'a')

ans = 'UNRESTORABLE'
for i in range(len(s)-len(T)+1):
  flg = True
  for j in range(len(T)):
    if s[i+j] == '?':
      continue
    if s[i+j] != T[j]:
      flg = False
      break
  if flg:
    ans = rplc(s, i , T) 

print(ans)

Solution (Python):
def solve(s, t):

    length = len(t)
    ans = []
    i = 0
    flg = False
    while i < len(s):
        tmp = s[i:i+length]
        count = len([i for i, j in zip(tmp, t) if i == j or i == "?"])
        if length == count:
            a = s[:i] + t + s[i+length:]
            ans.append(a.replace("?", "a"))

        i += 1

    return min(ans) if len(ans) > 0 else "UNRESTORABLE"

if __name__ == "__main__":
    s = input()
    t = input()
    print(solve(s, t))

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

kouho = []

for i in range(len(S) - len_T+1):
    
    flag = 0
    for a, b in zip(T, S[i:i+len_T]):
        if a == b or b == '?':
            continue
        else:
            flag = 1
    if flag == 1:
        continue
    
    kouho.append((S[:i]+T+S[i+len_T:]).replace('?', 'a'))
if not kouho:
    print("UNRESTORABLE")
else:
    print(sorted(kouho)[0])

Solution (Python):

s=input()
t=input()
ls=len(s)
lt=len(t)

cand=[]
for i in range(ls-lt+1):
    scut =s[i:i+lt]

    if all(tt==ss or ss=="?" for tt,ss in zip(t,scut) ):
        cand.append(i)
        
ans=[]
for c in cand:
    ans.append( (s[:c]+ t+ s[c+lt:]).replace("?","a") )

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

Solution (Python):
s = input()
t = input()
ss = []
for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if s[i+j] not in ('?', t[j]):
            break
    else:
        S = s[:i] + t + s[i+len(t):]
        ss.append(S.replace('?', 'a'))
if ss:
    print(min(ss))
else:
    print("UNRESTORABLE")

Solution (Python):
import copy

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

def construct(s, t, k):
	ans = copy.deepcopy(s)
	for i in range(len(t)):
		ans[i+k] = t[i]
	for i in range(len(s)):
		if ans[i] == '?':
			ans[i] = 'a'
	return "".join(ans)

def resolve():
	s = list(input())
	t = list(input())
	ans = []
	if len(s) < len(t):
		print('UNRESTORABLE')
		return
	for i in range(len(s) - len(t) + 1):
		if check(s, t, i):
			ans.append(construct(s, t, i))
	if len(ans) >= 1:
		print(min(ans))
	else:
		print('UNRESTORABLE')
resolve()

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

for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if s[i+j] not in ('?', t[j]):
            break
    else:
        S = s[:i] + t + s[i+len(t):]
        ss.append(S.replace('?', 'a'))

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

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    S_prime=readline().strip()
    T=readline().strip()

    U=list(S_prime)
    for i in range(len(U)):
        if U[i]=="?":
            U[i]="a"

    words=[]
    for i in range(len(S_prime)-len(T)+1):
        V=U[:]
        for j in range(i,i+len(T)):
            if S_prime[j]!=T[j-i]:
                if S_prime[j]!="?":
                    break
            V[j]=T[j-i]
        else:
            words.append("".join(V))

    words.sort()
    if len(words)>=1:
        print(words[0])
    else:
        print("UNRESTORABLE")

if __name__=="__main__":
    main()

Solution (Python):
import re

s2 = input().replace('?','.')
t = input()
a, b = len(s2), len(t)
ans = None

for i in range(a-b+1):
    s2_l = list(s2)
    t_l = list(t)
    if re.match(s2[i:i+b], t) is not None:
        for k in range(b):
            s2_l[i+k] = t_l[k]
        
        ans = "".join(s2_l).replace('.','a')
if ans is None:
    print('UNRESTORABLE')
else:
    print(ans)

Solution (Python):
import sys

S = list(sys.stdin.readline().rstrip())
T = list(sys.stdin.readline().rstrip())

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

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:
        for k in range(len_t):
            S[i+k] = T[k]

        print(("".join(S)).replace("?","a"))
        exit()

print("UNRESTORABLE")

Solution (Python):
chiper = input()
key = input()
save = []
cl = len(chiper)
kl = len(key)

def writeWord(ci):
    word=''
    for i in range(ci):
        if(chiper[i]=='?'):
            word += 'a'
        else:
            word += chiper[i]

    word += key
    for i in range(ci+kl,cl):
        if(chiper[i]=='?'):
            word += 'a'
        else:
            word += chiper[i]

    return word

def search():
    if(cl<kl):
        return
    last= cl-kl
    for i in range(last+1):
        for j in range(kl):
            if(chiper[i+j]=='?' or chiper[i+j]==key[j]):
                if(j==kl-1):
                    word = writeWord(i)
                    save.append(word)
            else:
                break
        
search()
if(len(save)==0):
    print('UNRESTORABLE')
else:
    save.sort()
    print(save[0])

Solution (Python):
S,T=input(),input();l,m=len(S),len(T)
for i in range(l-m+1):
 if all(c in"?"+d for c,d in zip(S[-i-m:],T)):S=S.replace("?","a");print(S[:-i-m]+T+S[l-i:]);quit()
print("UNRESTORABLE")

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

s = ''

slen = len(s_dash)
tlen = len(t)

k = None

for i in range(slen-tlen+1):
    for j in range(tlen):
        if s_dash[i+j] != t[j] and s_dash[i+j] != '?':
            break
    else:
        k = i

if k == None:
    print('UNRESTORABLE')
    exit()

for i in range(k):
    if s_dash[i] == '?':
        s = s + 'a'
    else:
        s = s + s_dash[i]
for i in range(tlen):
    s = s + t[i]
for i in range(k+tlen,slen):
    if s_dash[i] == '?':
        s = s + 'a'
    else:
        s = s + s_dash[i]
print(s)

Solution (Python):

import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    Sd = IS(str)
    T = IS(str)
    T_len = len(T)

    for i in range(len(Sd) - T_len, -1, -1):
        S_i = Sd[i: i + T_len]
        matched = all([s == '?' or s == t for s, t in zip(S_i, T)])
        if matched:
            left = ''.join(['a' if s == '?' else s for s in Sd[: i]])
            right = ''.join(['a' if s == '?' else s for s in Sd[i + T_len:]])
            print(left + T + right)
            exit()
    print('UNRESTORABLE')

solve()

Solution (Python):
s = str(input())
t = str(input())
lst = []
i= 0
for ss in range(len(s)-len(t)+1):
  flg = 1
  i= 0
  for tt in range(len(t)):
    now = ss + tt
    if s[now] == t[tt] or s[now] == '?':
      pass
    else:
      flg = 0
  if flg:
    ns = list(s)
    ns[ss:now+1] = list(t)
    lst.append(("").join(ns))
if lst:
  lst = [_.replace('?','a') for _ in lst]
  lst.sort()
  print(lst[0])

else:
  print('UNRESTORABLE')

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

if len(s_) < len(t):
    print("UNRESTORABLE")
    exit(0)

flag = True
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
    if flag is True:
        s_ = s_[0:i] + t + s_[i + len(t):]
        break

if flag is False:
    print("UNRESTORABLE")
else:
    print(s_.replace("?", "a"))

Solution (Python):



def can_contains(S, T):
  for s, t in zip(S, T):
    if s != '?' and s != t:
      return False

  return True


def main():
  Sd = input()
  T = input()

  len_T = len(T)

  if len(Sd) < len_T:
    print('UNRESTORABLE')
    return 

  for idx in range(len(Sd) - len_T, -1, -1):
    if can_contains(Sd[idx:idx + len_T], T):
      result = Sd.replace('?', 'a')
      result = result[:idx] + T + result[idx + len_T:] 
      
      print(result)
      return

  print('UNRESTORABLE')


if __name__ == '__main__':
  main()

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


i=len(Sd)-len(T)
j=0
ans=[]
flag=False
while(1):
    if i<0:
        
        break
    
    if Sd[i]==T[j] or Sd[i]=='?':
        
        if j==len(T)-1:
            ans.append((Sd[:i-j]+T+Sd[i-j+len(T):]).replace('?','a'))
            i-=j+1
            j=0
            flag=True
            continue
        j+=1
        i+=1
    else:
        i-=j+1
        j=0

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

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

b = []

for i in range(len(s)-len(t)+1):
    flag = True
    for j, st in enumerate(s[i:i+len(t)]):
        if not st == t[j] and not st == '?':
            flag = False
            break
    if flag:
        heapq.heappush(b,(s[:i]+t+s[i+len(t):]).replace('?','a'))

if not b:
    print('UNRESTORABLE')
else:
    print(heapq.heappop(b))

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

matches = []
for i in range(len(S) - len(T) + 1):
    index = i
    for j in range(len(T)):
        if S[index] == '?' or S[index] == T[j]:
            if j == len(T) -1:
                matches.append(i)
            index += 1
        else:
            break

if len(matches) == 0:
    print('UNRESTORABLE')
else:
    strings = []
    for i in matches:
        strings.append(''.join(S[:i] + T[:] + S[i+len(T):]).replace('?', 'a'))
    print(sorted(strings)[0])

Solution (Python):
import sys
input = sys.stdin.readline


def read():
    S = input().strip()
    T = input().strip()
    return S, T


def solve(S, T, INF = 100000):
    S = '.' + S
    T = '.' + T
    dp = [[INF for j in range(len(T))] for i in range(len(S))]
    
    offset = -1
    for i in range(len(S)):
        dp[i][0] = 0
    for j in range(len(T)):
        dp[0][j] = 0
    for i in range(1, len(S)):
        for j in range(1, len(T)):
            if S[i] == '?' or T[j] == '?' or S[i] == T[j]:
                dp[i][j] = dp[i-1][j-1] + 1
                if dp[i][j] == len(T)-1:
                    offset = i - (len(T)-1)
            else:
                dp[i][j] = 0
    
    if offset == -1:
        return 'UNRESTORABLE'
    U = [s if s != '?' else 'a' for s in S[1:]]
    for i in range(len(T)-1):
        U[offset + i] = T[i+1]
    return ''.join(U)
            

if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):
s=list(input())
t=list(input())
flag=False
ata=0
for i in range(len(s)-len(t)+1):
  if flag==True:
    break
  for j in range(len(t)):
    if s[len(s)-1-(i+j)]!="?" and s[len(s)-1-(i+j)]!=t[len(t)-1-j]:
      break
    if j==(len(t)-1):
      flag=True
      ata=i
if flag==True:
  for i in range(len(t)):
    s[len(s)-1-(ata+i)]=t[len(t)-1-i]
  for i in range(len(s)):
    if s[i]=="?":
      s[i]="a"
  print("".join(s))
else:
  print("UNRESTORABLE")

Solution (Python):
s=list(input())
t=list(input())
s_len=(len(s))
t_len=(len(t))

for i in range(s_len-t_len,-1,-1):
    for j in range(t_len):
        if s[i+j]==t[j] or s[i+j]=="?":
            continue
        else:
            break
    else:
        for k in range(t_len):
            s[i+k]=t[k]
        
        print(("".join(s)).replace("?","a"))
        exit()

print("UNRESTORABLE")

Solution (Python):
s=input()
t=input()
s_len=(len(s))
t_len=(len(t))

for i in range(s_len-t_len,-1,-1):
    for j in range(t_len):
        if s[i+j]!=t[j] and s[i+j]!="?":
            break
    else:
        print((s[:i]+t+s[i+t_len:]).replace("?","a"))
        exit()

print("UNRESTORABLE")

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

cand = []

for i in range(len(s)-len(t)+1):
    
    
    td = s[i: i+len(t)]
    
    for j in range(len(t)):
        if td[j] == '?':
            td = td[0:j] + t[j] +td[j+1:]
        elif td[j] != t[j]:
            break;
    
    if td == t:
        cand.append(s[0:i] + td + s[i+len(t):])


if len(cand) == 0:
    print('UNRESTORABLE')
else:
    cand = list(map(lambda x: x.replace('?', 'a'), cand))
    print(min(cand))

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

for i in range(s-t+1):
  if all(c in "?"+d for c,d in zip(S[-i-t:],T)):
    S=S.replace("?","a")
    print(S[:-i-t]+T+S[s-i:])
    quit()

print("UNRESTORABLE")

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

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

Solution (Python):
Sd = input()
T = input()
 
def check(A, B):
    for a, b in zip(A, B):
        if a != '?' and a != b:
            return False
 
    return True
 
for i in range(len(Sd) - len(T) + 1)[::-1]:
    if check(Sd[i:i + len(T)], T):
        print((Sd[:i] + T + Sd[i + len(T):]).replace('?', 'a'))
        break
else:
    print('UNRESTORABLE')

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


def isEqual(s1, s2):
    for i in range(len(s1)):
        if s1[i] != '?' and s1[i] != s2[i]:
            return False

    return True


kouhos = []
for i in range(len(S_hint)-len(T)+1):
    if isEqual(S_hint[i:i+len(T)], T):
        kouho = S_hint[0:i] + T + S_hint[i+len(T):]
        kouho = kouho.replace('?', 'a')
        kouhos.append(kouho)

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

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


def found(i):
    for j in range(len(T)):
        if S[i] == '?' or S[i] == T[j]:
            i += 1
        else:
            return False
    return True

ret = 'UNRESTORABLE'
for i in range(len(S) - len(T), -1, -1):
    if found(i):
        ret = S[:i] + T + S[i + len(T):]
        break
    else:
        i += 1

print(''.join('a' if x == '?' else x for x in ret))

Solution (Python):
s=input()
t=input()
ans=[]
for i in range(len(s)-len(t)+1):
  cnt=0
  for j in range(len(t)):
    if (s[i+j]==t[j]) or (s[i+j]=='?'):
      if j==len(t)-1:
        ss=s[:i]+t+s[i+j+1:]
        if ss.count('?')>0:
          ss=ss.replace('?','a')
        ans.append(ss)
      else:
        pass
    else:
      break
if ans!=[]:
  print(min(ans))
else:
  print('UNRESTORABLE')

Solution (Python):
import re
S2 = input()
T = input()

S2 = S2.replace('?', '.')
S2 = S2[::-1]
T = T[::-1]

ans = ""
i = 0
find = False
while i < len(S2):
    if find == False and len(T) > len(S2) - i:
        break
    p = S2[i:i+len(T)]
    m = re.match(p, T)
    if m and find == False:
        find = True
        ans = ans + T
        i += len(T)
    else:
        if S2[i] == '.':
            ans = ans + 'a'
        else:
            ans = ans + S2[i]
        i += 1

if len(ans) == len(S2):
    print(ans[::-1])
else:
    print('UNRESTORABLE')

Solution (Python):
s2 = input()
t = input()
c1=1
c2=0
c3=0

for i in range(len(s2)-len(t)+1):
    if s2[i]==t[0] or s2[i]=='?':
        for j in range(1,len(t)):
            if s2[i+j]==t[j] or s2[i+j]=='?':
                c1+=1
        if c1 == len(t):
            c2=1
            c3=i
        c1=1
if c2==1:
    s=list(s2)
    for i in range(len(t)):
        s[c3+i]=t[i]
    for i in range(len(s2)):
        if s[i]=='?':
            s[i]='a'
    print("".join(s))
else:
    print('UNRESTORABLE')

Solution (Python):
import copy

s = input().rstrip()
t = input().rstrip()
ns, nt = len(s), len(t)

start = -1
for i in range(ns - nt, -1, -1):
    for j in range(nt):
        if s[i+j] != t[j] and s[i+j] != '?':
            break
    else:
        start = i
        break

if start >= 0:
    s_ans = s[:start] + t + s[start+len(t):]
    print(s_ans.replace('?', 'a'))
else:
    print('UNRESTORABLE')

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

len_s = len(s)
len_t = len(t)


def search(s, t):
    head = len(s)-len(t)
    tail = len(s)
    for i in range(head+1):
        s_ = s[head:tail]
        
        for j in range(len(t)):
            if t[j] != s_[j]:
                if s_[j] != '?':
                    break
        
        else:
            return head, tail
        head -= 1
        tail -= 1
        continue
    return None, None


if len_s < len_t:
    print('UNRESTORABLE')
else:
    head, tail = search(s, t)
    if head is None:
        print('UNRESTORABLE')
    else:
        s = list(s)
        s[head:tail] = t
        s = "".join(s)
        s = s.replace('?', 'a')
        print(s)

Solution (Python):
import collections
def main():

    S = list(input())[::-1]
    T = list(input())[::-1]
    ary = []


    c = collections.Counter(S)
    if len(S) < len(T):
        print('UNRESTORABLE')
        return

    
    
    
    
    
    
    
    
    else:
        for s_left in range(len(S)):
            s_l = s_left
            flg = True
            tmp = S[::1]
            for t_left in range(len(T)) :
                if s_l < len(S) and (S[s_l] == T[t_left] or S[s_l] == '?'):
                    tmp[s_l] = T[t_left]
                    s_l += 1
                    continue
                else:
                    flg = False
                    break
            if flg:
                ans_t = tmp[::-1]
                ans = ''
                for i in range(len(S)):
                    if ans_t[i] == '?':
                        ans += 'a'
                    else:
                        ans += ans_t[i]
                
                
                ary.append(ans)

        if len(ary) == 0:

            print('UNRESTORABLE')
            return
        ary.sort()
        print(ary[0])

if __name__ == '__main__':
    main()

Solution (Python):
import collections
def main():

    S = list(input())[::-1]
    T = list(input())[::-1]
    ary = []


    c = collections.Counter(S)
    if len(S) < len(T):
        print('UNRESTORABLE')
        return

    
    
    
    
    
    
    
    
    else:
        for s_left in range(len(S)):
            s_l = s_left
            flg = True
            tmp = S[::1]
            for t_left in range(len(T)) :
                if s_l < len(S) and (S[s_l] == T[t_left] or S[s_l] == '?'):
                    tmp[s_l] = T[t_left]
                    s_l += 1
                    continue
                else:
                    flg = False
                    break
            if flg:
                ans_t = tmp[::-1]
                ans = ''
                for i in range(len(S)):
                    if ans_t[i] == '?':
                        ans += 'a'
                    else:
                        ans += ans_t[i]
                
                
                ary.append(ans)

        if len(ary) == 0:

            print('UNRESTORABLE')
            return
        ary.sort()
        print(ary[0])

if __name__ == '__main__':
    main()

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

def main():
    S = readline().rstrip()
    T = readline().rstrip()
    N = len(S)
    M = len(T)
    for i in range(N-M, -1, -1):
        s = S[i:i+M]
        flg = True
        for e1, e2 in zip(s, T):
            if e1 != '?' and e1 != e2:
                flg = False
                break
        if flg:
            ans = list(S)
            for j in range(i, i+M):
                ans[j] = T[j-i]
            ans = ''.join(ans)
            print(ans.replace('?', 'a'))
            return

    print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Solution (Python):
s2 = input()
t = input()
c1=1
c2=0
c3=0

for i in range(len(s2)-len(t)+1):
    if s2[i]==t[0] or s2[i]=='?':
        for j in range(1,len(t)):
            if s2[i+j]==t[j] or s2[i+j]=='?':
                c1+=1
        if c1 == len(t):
            c2=1
            c3=i
        c1=1
if c2==1:
    s=list(s2)
    for i in range(len(t)):
        s[c3+i]=t[i]
    for i in range(len(s2)):
        if s[i]=='?':
            s[i]='a'
    print("".join(s))
else:
    print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()
same = lambda x, y: all(True if i == j or i == '?' else False for i, j in zip(x, y))
n = len(T)
for i in range(len(S)-n, -1, -1):
    if same(S[i:i+n], T):
        S = S[:i] + T + S[i+n:]
        break
else:
    print('UNRESTORABLE')
    exit()
print(S.replace('?', 'a'))

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

Solution (Python):
inf = 10**15
mod = 10**9+7
s = input()
t = input()
lenn = len(s)
lent = len(t)
ans = []
for i in range(lenn-lent+1):
    flag = True
    tmp = ['' for i in range(lenn)]
    for j,k in enumerate(range(i,i+lent)):
        if (s[k] != t[j]) and (s[k] != '?'):
            flag = False
            break
        else:
            tmp[k]=t[j]
    if flag:
        for l in range(lenn):
            if tmp[l] == '':
                if s[l] == '?':
                    tmp[l] = 'a'
                else:
                    tmp[l] = s[l]
        ans.append(''.join(tmp))
ans.sort()
if ans:
    print(ans[0])
else:
    print('UNRESTORABLE')

Solution (Python):
import sys

S = list(input())
T = list(input())

length = len(T)
position = len(S) - length

while position >= 0:
    text = S[position:position+length]
    for i in range(length):
        if text[i] == "?":
            text[i] = T[i]
    if text != T:
        position -= 1
    else:
        S[position:position+length] = T
        position = -10

if position != -10:
    print("UNRESTORABLE")
    sys.exit()

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

print("".join(S))