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 zehnpaard():
    s = input()
    t = input()

    def match(s1, t):
        return all(a in (b, '?') for a, b in zip(s1, t))

    substrings = [s[i:i+len(t)] for i in range(len(s)-len(t)+1)]
    res = 'UNRESTORABLE'
    for ss in reversed(substrings):
        if match(ss, t):
            left, _, right = s.rpartition(ss)
            res = left + t + right
            res = res.replace('?', 'a')
            break
    print(res)

def main4():
    s = input() 
    t = input() 

    def match(sub, t):
        for i in range(len(t)):
            if sub[i] == "?":
                continue
            if sub[i] == t[i]:
                continue
            return False
        return True

    substrings = []
    for i in range(len(s)-len(t)+1):
        substrings.append(s[i:i+len(t)])
    

    for sub in reversed(substrings):
        if match(sub, t):
            left, _, right = s.rpartition(sub)
            result = (left + t + right).replace("?", "a")
            print(result)
            exit()
    print('UNRESTORABLE')


def main3():
    def product_mask_s(s, c):
        import itertools
        s_products = list(itertools.product(s, c))

        s_masks = []
        len_s = len(s)
        for products in itertools.product(range(2), repeat=len_s):
            t = ""
            for i in range(len_s):
                t += s_products[i][products[i]]
            s_masks.append(t)
        return s_masks

def main2():
    """ TLE WA """
    
    
    
    import itertools
    s_prime = input()
    t_origin = input()
    len_t = len(t_origin)
    t_products = list(itertools.product(t_origin, "?"))

    t_kouho = []
    for products in itertools.product(range(2), repeat=len_t):
        t = ""
        for i in range(len_t):
            t += t_products[i][products[i]]
        t_kouho.append(t)

    for t in t_kouho:
        find_i = s_prime.find(t)
        if find_i == -1:
            continue
        
        ans = "{}{}{}".format(s_prime[:find_i], t_origin, s_prime[find_i+len_t:])
        print(ans.replace("?", "a"))
        exit()
    print("UNRESTORABLE")


def main():
    """ WA """
    s_change = lambda s, i, c: "{}{}{}".format(s[:i-1], c, s[i-1+1:])

    s_prime = input() 
    t_origin = input()       

    
    len_t = len(t_origin)
    t = t_origin

    t_kouho = [t]
    for t_i in range(1, len_t):
        t_tail = s_change(t, len_t-t_i+1, "?")
        t_head = s_change(t, t_i, "?")
        if t_tail == t_head:
            t_kouho.append(t_tail)
        else:
            t_kouho.append(t_tail)
            t_kouho.append(t_head)
        t = s_change(t_tail, t_i, "?")
        if t == "?"*len_t:
            break
    print(t_kouho)

    for t in t_kouho:
        t_i = s_prime.find(t)
        if t_i == -1:
            continue
        
        ans = "{}{}{}".format(s_prime[:t_i], t_origin, s_prime[t_i+len_t:])
        print(ans.replace("?", "a"))
        exit()
    print("UNRESTORABLE")

if __name__ == '__main__':
    main4()

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

n = len(s)
delta = len(t)
ans_list = []

for i in range(n-delta+1):
	for j in range(delta):
		if s[i+j] == '?' or s[i+j] == t[j]:
			pass
		else:
			break
	else:
		ans_list.append((s[:i]+t+s[i+j+1:]).replace('?', 'a'))

ans_list.sort()

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

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

Solution (Python):
s = input()
t = input()
start = -1
for i in range(len(s)-len(t)+1):
    flag = 1
    for j in range(len(t)):
        if s[i+j] != t[j] and s[i+j] != "?":
            flag = 0
    if flag:
        start = i

if start == -1:
    print("UNRESTORABLE")
else:
    ans = s[:start] + t + s[start+len(t):]
    for i in ans:
        if i == "?":
            print("a", end="")
        else:
            print(i, end="")
    print()

Solution (Python):

S_dash=str(input())
T=str(input())
location=-1
for i in range(len(S_dash)-len(T)+1):
    j=0
    while j<=len(T)-1:
        if S_dash[i+j]==T[j] or S_dash[i+j]=='?':
            if j==len(T)-1:
                location=i
            j+=1
        else:
            break
if location>=0:
    S_dash_list=list(S_dash)
    for i in range(len(T)):
        S_dash_list[location+i]=T[i]
    S_tmp="".join(S_dash_list)
    S=S_tmp.replace('?','a')
if location>=0:
    print(S)
else:
    print('UNRESTORABLE')

Solution (Python):
def c_dubious_document_2(S, T):
    
    
    candidate = []  
    for i in range(len(S) - len(T) + 1):
        
        for j in range(len(T)):
            if S[i + j] != '?' and S[i + j] != T[j]:
                break  
        else:
            
            candidate.append(S[:i] + T + S[i + len(T):])
    if len(candidate) == 0:
        ans = 'UNRESTORABLE'
    else:
        tmp = []
        for s in candidate:
            tmp.append(s.replace('?', 'a'))
        ans = sorted(tmp)[0]
    return ans

S = input()
T = input()
print(c_dubious_document_2(S, T))

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

answers = []
for i in range(len(s_)-len(t)+1):
    ans = s_
    for j in range(len(t)):
        matched = True
        if s_[i+j] != t[j] and s_[i+j] != '?':
            matched = False
            break
        else:
            ans = ans[:i+j] + t[j] + ans[i+j+1:]
    if matched:
        ans = ans.replace('?', 'a')
        answers.append(ans)


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

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

start = -1
for i in range(len(S)-len(T)+1):
    match = True
    for j in range(len(T)):
        if not (S[i+j] == T[j] or S[i+j] == "?"):
            match = False
    if match:
        start = i


if start == -1:
    print('UNRESTORABLE')
else:
    result = S[:start] + T + S[start+len(T):]
    print(result.replace("?", "a"))

Solution (Python):
import sys

def is_macth(x, t, n):
    for i in range(n):
        if x[i] != '?' and x[i] != t[i]:
            return False
    return True


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

if len_s < len_t:
    print('UNRESTORABLE')
    sys.exit(0)

segs = [s[i:i+len_t] for i in range(len_s - len_t + 1)]
ans = 'z' * 51

for i, seg in enumerate(segs):
    if is_macth(seg, t, len_t):
        tmp = s[0:i] + t + s[i+len_t:]
        tmp = tmp.replace('?', 'a')
        ans = min(ans, tmp)

if len(ans) > 50:
    print('UNRESTORABLE')
else:
    print(ans)

Solution (Python):
S = input()
T = input()
T_high = len(T) - 1

ans = "~~UNRESTORABLE~~"

for i in range(T_high, len(S)):
    flg = False
    for j in range(T_high + 1):
        c = S[i + j - T_high]
        if c != '?' and c != T[j]:
            flg = True
            break
    if not flg:
        buf = S.replace("?", "a")
        ans = min(ans, buf[:(i - T_high)] + T + buf[(i + 1):])

print(ans.replace("~", ""))

Solution (Python):
S = input()
T = input()
T_high = len(T) - 1

ans = []

for i in range(T_high, len(S)):
    flg = False
    for j in range(T_high + 1):
        c = S[i + j - T_high]
        if c != '?' and c != T[j]:
            flg = True
            break
    if not flg:
        buf = S.replace("?", "a")
        ans.append(buf[:(i - T_high)] + T + buf[(i + 1):])

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

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

answer = ''
for i in range(len(S)-len(T)+1):
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = (S[:i] + T + S[i+len(T):]).replace('?', 'a')
    if answer == '' or tmp < answer:
        answer = tmp

print('UNRESTORABLE' if answer == '' else answer)

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

answer = ''
for i in range(len(S)-len(T)+1):
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = (S[:i] + T + S[i+len(T):]).replace('?', 'a')
    if answer == '' or tmp < answer:
        answer = tmp 

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

Solution (Python):
from copy import deepcopy

S = input()
T = input()

answer = ''
for i in range(len(S)-len(T)+1):
    found = True
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = deepcopy(S).replace('?', 'a')
    tmp = tmp[:i] + T + tmp[i+len(T):]
    if answer == '' or tmp < answer:
        answer = tmp

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

Solution (Python):
from copy import deepcopy

S = input()
T = input()

answer = ''
for i in range(len(S)-len(T)+1):
    found = True
    for j in range(len(T)):
        if S[i+j] != '?' and S[i+j] != T[j]:
            found = False
            break
    if found:
        tmp = deepcopy(S).replace('?', 'a')
        tmp = tmp[:i] + T + tmp[i+len(T):]
        if answer == '' or tmp < answer:
            answer = tmp

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

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

len_SS = len(SS)
len_T = len(T)

flag = False
for i in range(len_SS - len_T + 1):
    window = SS[len_SS - len_T - i:len_SS - i]
    matching = True
    for j in range(len_T):
        if not window[j] == "?":
            if window[j] != T[j]:
                matching = False
                break

    if matching:
        flag = True
        break

if flag:
    SS = list(SS)
    SS[len_SS - len_T - i:len_SS - i] = list(T)
    SS = "".join(SS)
    SS = SS.replace("?", "a")
    print(SS)

else:
    print("UNRESTORABLE")

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

    ans = []
    for i in range(len(S) - len(T) + 1):
        ok = True
        for j in range(len(T)):
            if not(S[i + j] == T[j] or S[i + j] == "?"):
                ok = False
        if ok:
            ans.append(S[:i] + T + S[i + len(T):])

    if len(ans) == 0:
        print("UNRESTORABLE")
    else:
        t = min(ans)
        print(t.replace("?", "a"))


if __name__ == '__main__':
    main()

Solution (Python):
S=list(input())
T=list(input())
A=[]
for i in range(len(S)-len(T)+1):
    is_ok=1
    for j in range(i, i+len(T)):
        if S[j]!=T[j-i] and S[j]!='?':
            is_ok=0
    if is_ok:
        tmp=S[:]
        is_con=1
        for j in range(i, i+len(T)):
            tmp[j]=T[j-i]
        A.append(tmp)
if len(A)==0:
    print("UNRESTORABLE")
    exit(0)
B=[]
for a in A:
    for i in range(len(a)):
        if a[i]=='?':
            a[i]='a'
    B.append(''.join(a))
B.sort()
print(B[0])

Solution (Python):
Sd=input().strip()
T=input().strip()
S=""
for i in range(len(Sd)-len(T),-1,-1):
    sub=Sd[i:i+len(T)]
    for t,s in zip(T,sub):
        if s!="?" and s!=t:
            break
    else:
        S=Sd[:i]+T+Sd[i+len(T):]
        break
if S != "":
    print(S.replace("?", "a"))
else:
    print("UNRESTORABLE")

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

STRMAX = "z" * 50
ans = STRMAX
for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            if j == len(T)-1:
                work = S[:i] + T + S[i+j+1:]
                work = work.replace('?','a')
                if work < ans:
                    ans = work
        else:
            break

if ans == STRMAX:
    print("UNRESTORABLE")
else:
    print(ans)

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

ans = 'UNRESTORABLE'
for i in range(len(s) - len(t), -1, -1):
    ds = s[i:i + len(t)]
    flg = True
    for j in range(len(t)):
        if (ds[j] != t[j]) and (ds[j] != '?'):
            flg = False
            break
    if flg:
        ans = s[:i] + t + s[i + len(t):]
        ans = ans.replace('?', 'a')
        break
print(ans)

Solution (Python):
res = 'z'*100
s = input()
t = input()
l = len(t)
for i in range(len(s)-l+1):
    string = s[i:i+l]
    flag = False
    for (j,k) in zip(string,t):
        if j!=k and j!='?':
            flag = True
            break
    if flag:
        continue
    tmp = s[:i] + t + s[i+l:]
    res = min(res, tmp.replace('?','a'))
if res=='z'*100:
    print('UNRESTORABLE')
else:
    print(res)

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

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

ss = []
for i in range(0, len(sd) - len(t) + 1):
    if coincide(sd[i:i+len(t)], t):
        ss.append((sd[0:i]+t+sd[i+len(t):]).replace('?', 'a'))
    else:
        continue

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

Solution (Python):
import re

S = input().replace('?', '.')
T = input()

ans = []

for i in range(len(S)-len(T), -1, -1):
    if re.match(S[i:i+len(T)], T):
        ans.append((S[:i] + T + S[i+len(T):]).replace('.', 'a'))

print(min(ans) if ans else 'UNRESTORABLE')

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

a = [] 
tl = len(t)
sl = len(s)

for i in range(sl-tl+1):
    k = 0
    for j in range(tl):
        if t[j] == s[i+j] or s[i+j] == '?':
            k += 1
        else:
            break
    if k == tl:
        if i == 0 and i == sl-tl:
            w = t
        elif i == 0:
            w =  t + ['a' if hj == '?' else hj for hj in s[i+tl::]]
        elif i == sl-tl:
            w =  ['a' if hj == '?' else hj for hj in s[:i:]] + t
        else:
            w = ['a' if hj == '?' else hj for hj in s[:i:]] + t + ['a' if hj == '?' else hj for hj in s[i+tl::]]
        ass = ''
        for jkl in range(len(w)):
            ass += w[jkl]
        a.append(ass)
        


if len(a)== 0:
    print('UNRESTORABLE')
else:
    print(min(a))

Solution (Python):
def judge(Sd:str, T:str) -> str:
    lenS = len(Sd)
    lenT = len(T)
    
    
    start = -1
    for i in range(lenS - lenT + 1):
        isMatch = True
        for j in range(lenT):
            if not (Sd[i+j] == T[j] or Sd[i+j] == "?"):
                isMatch = False
            
        if isMatch:
            start = i
            
    if start == -1:
        return "UNRESTORABLE"
    
    
    
    S = Sd[:start] + T + Sd[start+lenT:]
    ans = ""
    for i in range(lenS):
        if S[i] == "?":
            ans = ans + "a"
        else:
            ans = ans + S[i]
            
    return ans
            


Sd = input()
T = input()

S = judge(Sd, T)

print(S)

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

for i, s_i in tuple(zip(range(len(s_)-len(t)+1), [s_[x:x+len(t)] for x in range(len(s_)-len(t)+1)]))[::-1]:
    if all((s_i_c == "?" or s_i_c == t[j]) for j, s_i_c in enumerate(s_i)):
        print((s_[:i] + t + s_[i+len(t):]).replace("?", "a"))
        exit()
print("UNRESTORABLE")

Solution (Python):

def reverse_enumerate(l):
	for i, l_i in zip(range(len(l)-1, -1, -1), l):
		yield i, l_i

def n_gram(s, n):
	return [s[i:i+n] for i in range(len(s)-n+1)]

def is_equal_match(s1, s2, all_match=[]):
	for i in range(len(s1)):
		if not (s1[i] == s2[i] or s1[i] in all_match):
			return False
	return True

def fill_in_str(text, s, n):
	return text[:n] + s + text[n+len(s):]

s_ = input()
t = input()

for i, s_i in reverse_enumerate(n_gram(s_, len(t))[::-1]):
	if is_equal_match(s_i, t, all_match="?"):
		print(fill_in_str(s_, t, i).replace("?", "a"))
		exit()
print("UNRESTORABLE")

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

for i, s_i in tuple(zip(range(len(s_)-len(t)+1), [s_[x:x+len(t)] for x in range(len(s_)-len(t)+1)]))[::-1]:
	if all((s_i_c == "?" or s_i_c == t[j]) for j, s_i_c in enumerate(s_i)):
		print(s_[:i].replace("?", "a") + t + s_[i+len(t):].replace("?", "a"))
		exit()
print("UNRESTORABLE")

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

answer = []
for i in range(len(s)-len(t)+1):
    a = 0
    for j in range(len(t)):
        if s[i+j]=='?' or s[i+j]==t[j]:
            a += 1
    if a == len(t):
        ans = []
        for k in range(len(s)):
            if i <= k < i + len(t):
                ans.append(t[k-i])
            elif s[k]=='?':
                ans.append('a')
            else:
                ans.append(s[k])
        ans = ''.join(ans)
        answer.append(ans)
if len(answer)==0:
    print('UNRESTORABLE')
else:
    print(sorted(answer)[0])

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

Solution (Python):


def main():
    s = input()
    t = input()
    i = -1

    for j in range(len(s) - len(t) + 1):
        if all(a == '?' or a == b for a, b in zip(s[j:], t)):
            i = j
    if i == -1:
        print('UNRESTORABLE')
    else:
        ans = s[:i] + t + s[i+len(t):]
        ans = ans.replace('?', 'a')
        print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
S = list(input())
t = input()
T = list(t)
NS = len(S)
NT = len(T)
j = -1
if NS < NT:
    pass
else:
    for i in range(NS - NT,-1,-1):
        j = i
        for k in range(NT):
            if S[i+k] == T[k] or S[i+k] == '?':
                pass
            else:
                j = -1
                break
        if j != -1:
            break
            
if j == -1:
    print('UNRESTORABLE')
else:
    k = 0
    ans = ''
    while k != NS:
        if k == j:
            ans += t
            k += NT
        else:
            if S[k] == '?':
                ans += 'a'
            else:
                ans += S[k]
            k += 1
    print(ans)

Solution (Python):
def chk(a,b):
    for (i,j) in zip(a,b):
        if i!=j and i!="?":
            return False
    return True

s=input()
t=input()
n=len(t)
l=[]
for i in range(len(s)-n+1):
    if chk(s[i:i+n],t):
        l.extend([s[:i]+t+s[i+n:]])
if not l:
    print("UNRESTORABLE")
else:
    l=sorted(l)
    print(l[0].replace("?","a"))

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

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

ans = 'UNRESTORABLE'
for i in sorted(range(ls - lt + 1), reverse=True):
    ds = s[i:i+lt]
    flg = True
    for j in range(lt):

        if (ds[j] != t[j]) and (ds[j] != '?'):
            flg = False
            break
    if flg:
        ans = s[:i] + t + s[i+lt:]
        ans = ans.replace('?', 'a')
        break
print(ans)

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

search_str = ["?" for x in range(len(T))]

last_occ = -1

for i in range(len(S)) :    
    if i < len(S) - len(T) + 1:
        ok_cnt = 0
        for j in range(len(T)) :
            if S[i + j] == T[j] or S[i + j] == "?" :
                ok_cnt += 1

        if ok_cnt == len(T) :
            last_occ = i
            
if last_occ != -1 :
    str_tmp = S[0:last_occ] + T + S[last_occ + len(T):]
    print(str_tmp.replace("?", "a"))
else :
    print("UNRESTORABLE")

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

s2.reverse() 
t.reverse() 

ans = 'UNRESTORABLE'

cands = []

for i in range(len(s2) - len(t) + 1):
    for j in range(len(t)):
        if t[j] == s2[i + j] or s2[i + j] == '?':
            if j == len(t) - 1:
                s2[i:i+j + 1] = t
                s2.reverse()
                ans = ''.join(s2)
                ans = ans.replace('?', 'a')
                cands.append(ans)
                break
            else:
                continue
        else:
            break

if cands:
    print(sorted(cands)[0])
else:
    print('UNRESTORABLE')

Solution (Python):



S_d = input()
T = input()
len_T = len(T)


def replacable(string, T):
    for char_s, char_t in zip(string, T):
        if char_s == char_t or char_s == '?':
            continue
        else:
            return False
    else:
        return True



t_start_index_list = []

for start_idx in range(len(S_d) - len_T + 1):
    string = S_d[start_idx:start_idx + len_T]
    
    if replacable(string, T):
        t_start_index_list.append(start_idx)

if len(t_start_index_list) == 0:
    print("UNRESTORABLE")
else:
    S_candidate_list = []
    for t_start_index in t_start_index_list:
        S = S_d[:t_start_index] + T + S_d[t_start_index + len_T:]
        S = S.replace('?', 'a')
        S_candidate_list.append(S)
    S_candidate_list.sort()
    print(S_candidate_list[0])

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

Solution (Python):
import re
sd = input()
ts =  input()

def f():
    cand = []
    for i in range(len(sd) - len(ts) + 1):
        p = sd[i:i + len(ts)]
        pat = re.compile(p.replace('?', '.'))
        if re.match(pat, ts):
            cand.append((sd[:i] + ts + sd[i+len(ts):]).replace('?', 'a'))
    return sorted(cand)[0] if cand else 'UNRESTORABLE'
print(f())

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

N = len(S2)


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


S = 'UNRESTORABLE'
if len(T) <= N:
    C = []
    for i in range(N - len(T) + 1):
        j = i + len(T)
        if (S2[i] == T[0] or S2[i] == '?') and check(S2[i:j], T):
            S = list(S2)
            S[i:j] = list(T)
            S = ''.join(S).replace('?', 'a')
            C.append(S)
    if len(C) > 0:
        S = sorted(C)[0]
print(S)

Solution (Python):
S_prime = input()
T = input()
C = []
for i in range(len(S_prime) - len(T) + 1):
  match = True
  for j in range(len(T)):
    if S_prime[i + j] == '?':
      continue
    elif S_prime[i + j] != T[j]:
      match = False
      break
  if match:
    C.append(i)
Ss = []
for c in C:
  S = ''
  for i, s in enumerate(S_prime):
    if i < c or i >= c + len(T):
      S += s if s != '?' else 'a'
    elif i == c:
      S += T
  Ss.append(S)
if Ss:
  print(sorted(Ss)[0])
else:
  print('UNRESTORABLE')

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

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

def pad(S,T,idx):
	tmp = ""
	for i in range(len(S)):
		if i < idx:
			tmp += "a" if S[i] == "?" else S[i]
		elif i < idx + len(T):
			tmp += T[i-idx]
		else:
			tmp += "a" if S[i] == "?" else S[i]
	return tmp

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


a = []
for i in range(len(S)-len(T)+1):
	if check(S[i:i+len(T)],T) == True:
		a.append(pad(S,T,i))
if len(a) == 0:
	print('UNRESTORABLE')
	exit()

print(sorted(a)[0])

Solution (Python):
import re
sd = input()
ts =  input()

def f():
    for i in range(len(sd) - len(ts) + 1):
        p = sd[len(sd) - i - len(ts):len(sd) - i]
        pat = re.compile(p.replace('?', '.'))
        if re.match(pat, ts):
            lsd = list(sd)
            lsd[len(sd) - i - len(ts):len(sd) - i] = list(ts)
            print(''.join(lsd).replace('?', 'a'))
            return
    print('UNRESTORABLE')
f()

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

a=len(s)
b=len(t)
f=0
for i in range(-1,-a+b-2,-1):
    f=0
    for j in range(-1,-b-1,-1):
        if (s[i+j+1]!="?"and t[j]!=s[i+j+1]):
            f=1
            break
    if f==1: continue
    for k in range(-1,-b-1,-1):
        s[i+k+1]=t[k]
    for p in range(a):
        if s[p]=="?":
            s[p]="a"
    print("".join(s))
    exit()
print("UNRESTORABLE")

Solution (Python):
import sys
s = raw_input()
t = raw_input()

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

for i in range(ls-lt,-1,-1):
	count = 0
	for j in range(lt):
		if s[i+j] == '?' or s[i+j] == t[j]:
			count += 1
		else :
			break
	if count == lt:

		print s[:i].replace('?','a')+t+s[i+lt:].replace('?','a')
		sys.exit()

print 'UNRESTORABLE'

Solution (Python):


S2 = input()
T = input()


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

print("UNRESTORABLE")

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

insert_index = -1
while True:

    flag = True

    if left + len(t) - 1 == len(s):
        break

    for a, b in zip(s[left:left+len(t)], t):
        if a != b and a != "?":
            flag = False
            break
    if flag:
        insert_index = max(insert_index, left)
    left += 1


if insert_index == -1:
    print("UNRESTORABLE")
else:
    next_word = s[:insert_index] + t + s[insert_index+len(t):]

    ans = next_word.replace("?", "a")
    print(ans)

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

Solution (Python):
S=input()
T=input()
candidates=[]
strings=[]
for i in range(len(S)-len(T)+1):
    flag=1
    for j in range(len(T)):
        if S[i+j]=='?':
            pass
        else:
            if S[i+j]!=T[j]:
                flag=0
                break
            else:
                pass
    if flag:
        replacement=S[:i]+T+S[i+len(T):]
        strings.append(replacement)

for string in strings:
    s=''
    for i in range(len(string)):
        if string[i]=='?':
            s=s+'a'
        else:
            s=s+string[i]
    candidates.append(s)

if len(candidates)==0:
    print("UNRESTORABLE")
    quit()
elif len(candidates)==1:
    print(candidates[0])
    quit()
else:
    candidates.sort()
    print(candidates[0])
    quit()

Solution (Python):
S = input()
T = input()
if len(T) > len(S):
    print('UNRESTORABLE')
    exit()

cands = []
for i in range(len(S)-len(T)+1):
    for c1,c2 in zip(T,S[i:]):
        if c1 == c2: continue
        if c2 == '?': continue
        break
    else:
        tmp = S[:i] + T + S[i+len(T):]
        cands.append(tmp.replace('?','a'))
if cands:
    print(sorted(cands)[0])
else:
    print('UNRESTORABLE')

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

	for i in reversed(range(len(s) - len(t) + 1)):
		if checker(s[i:(i + len(t))], t):
			s = s.replace("?", "a")
			print(s[:i] + t + s[i+len(t):])
			return
	print("UNRESTORABLE")

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

if __name__ == '__main__':
	main()

Solution (Python):
s = input()
t = input()
a =[]
for i in range(len(s)-len(t)+1):
    f = True
    for j in range(len(t)):
        if s[i+j] != '?' and s[i+j] != t[j]:
            f = False
    if f == True:
        a.append(i)

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

Solution (Python):

SD = list(input())
T = list(input())

S = []
flag = -1

for i in range(len(SD)-len(T), -1, -1):
    Sub_S = SD[i:i+len(T)]
    for t,s in zip(T, Sub_S):
        if s!="?" and s!=t:
            break
    else:
        S=SD[:i]+T+SD[i+len(T):]
        flag = 1
        break
    
if flag<0:
    print("UNRESTORABLE")
else:
    for i in range(len(SD)):
        if S[i]=="?":
            S[i]="a"

print("".join(S))

Solution (Python):
def check(x,y,n):
	for i in range(n):
		if x[i] != '?' and y[i] != x[i]:
			return False
	return True
			
S = list(input())
T = list(input())
S_len = len(S)
T_len = len(T)

if T_len > S_len:
	print('UNRESTORABLE')
	quit()
	
answer = []

for i in range(S_len - T_len + 1):
	S_dush = S[i:i+T_len]
	if check(S_dush,T,T_len):
		ans = S[:]
		ans[i:i+T_len] = T
		for j in range(S_len):
			if ans[j] == '?':
				ans[j] = 'a'
		answer.append(''.join(ans))
if answer:
	answer.sort()
	print (answer[0])
else:
	print('UNRESTORABLE')

Solution (Python):
s = list(input())
slen = len(s)
t = list(input())
tlen = len(t)
box = []


def check(wh, phase=0):
    if wh + phase >= slen:
        return False
    if s[wh + phase] == "?" or s[wh + phase] == t[phase]:
        if phase + 1 == tlen:
            return True
        return check(wh, phase + 1)
    return False


def ume(arr):
    tmp = [e for e in arr]
    for i, e in enumerate(arr):
        if e == "?":
            tmp[i] = "a"
    return tmp



for i, e in enumerate(s):
    if check(i):
        box.append(ume(s[:i] + t + s[i + tlen:]))

if not box:
    print("UNRESTORABLE")
else:
    box.sort
    print("".join(box[-1]))

Solution (Python):
s = input()
t = input()
ans = ""
po = False

for i in range(len(s)-len(t)+1):
    if s[i] in [t[0],"?"]:
        if all([s[i + j] in [t[j], "?"]for j in range(len(t))]):
            if not po:
                ans = (s[:i] + t + s[i + len(t):]).replace("?","a")
                po = True
            temp = (s[:i] + t + s[i + len(t):]).replace("?","a")
            if temp < ans:
                ans = temp
            


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

Solution (Python):
s = []
s += input()
t = []
t += input()
u = -1
for i in range(len(s) - len(t) + 1):
    no = 0
    if s[i] == t[0] or s[i] == "?":
        for k in range(len(t) - 1):
            if s[i + k + 1] != t[k + 1] and s[i + k + 1] != "?":
                no = 1
                break
        if no == 0:
            u = i
if u == -1:
    print("UNRESTORABLE")
else:
    for i in range(len(s)):
        if u <= i <= u + len(t) - 1:
            s[i] = t[i-u]
        elif s[i] == "?":
            s[i] = "a"
    print("".join(s))