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

Solution (Python):
Sd=input().strip()
T=input().strip()
S=None
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
print(S.replace("?","a") if S else "UNRESTORABLE")

Solution (Python):
s = input()
t = input()
ans = ''
if len(s) >= len(t):
    for i in range(len(s)-len(t), -1, -1):
        s_tmp = s[i:i+len(t)]
        flag = 0
        for j in range(len(t)):
            if s_tmp[j] == '?':
                s_tmp = t[:j+1] + s_tmp[j+1:]
            elif s_tmp[j] != t[j]:
                flag = 1
                break
        if flag == 0:
            ans = s[:i] + t + s[i+len(t):]
            break
else:
    flag = 1

if flag == 0:
    for i in range(len(ans)):
        if ans[i] == '?':
            ans = ans[:i] + 'a' + ans[i+1:]
    print(ans)
else:
    print('UNRESTORABLE')

Solution (Python):
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)

Solution (Python):
s=input()
t=input()
if len(s)<len(t):
    print("UNRESTORABLE")
else:
    x=[]
    for i in range(len(s)-len(t)+1):
        k=0
        for j in range(len(t)):
            if s[i+j]!=t[j] and s[i+j]!="?":
                k+=1
        if k==0:
            x.append((s[:i]+t+s[(i+len(t)):]).replace("?","a"))
    if len(x)==0:
        print("UNRESTORABLE")
    else:
        x.sort()
        print(x[0])

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


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

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

ans = []
sl = len(S)
tl = len(T)
for i in range(sl-tl+1):
	if S[i] == '?' or S[i] == T[0]:
		flg = True
		for j in range(1,tl):
			if S[i+j] != '?' and S[i+j] != T[j]:
				flg = False
				break

		if flg:
			s = S[:i] + T + S[i+tl:]
			ans.append(s.replace('?','a'))

if ans == []:
	print("UNRESTORABLE")
	exit(0)

ans.sort()
print(ans[0])

Solution (Python):
S = input()
T = input()
len_s = len(S)
len_t = len(T)
candidates = []
for i in range(len_s - len_t + 1):
    insertable = True
    for j in range(len_t):
        if S[i+j] != "?" and T[j] != S[i+j]:
            insertable = False
    if insertable:
        u = ""
        for k in range(len_s):
            if k >=i and k <= i + len_t - 1:
                u+=T[k-i]
            else:
                if S[k] == "?":
                    u+="a"
                else:
                    u+=S[k]
        candidates.append(u)   
if not candidates:
    print("UNRESTORABLE")
else:
    candidates.sort()
    print(candidates[0])

Solution (Python):
import sys

S_dash = input()
T = input()

for i in range(len(S_dash) - len(T), -1, -1):
    for idx, s in enumerate(T):
        if S_dash[i + idx] != s and S_dash[i + idx] != '?':
            break
    else:
        S = S_dash[:i] + T + S_dash[i + len(T):]
        S = S.replace('?', 'a')
        print(S)
        sys.exit()
print('UNRESTORABLE')

Solution (Python):

S = input()
T = input()

s = len(S)
t = len(T)
ans = 'UNRESTORABLE'

if len(T) <= len(S):
    for i in range(s - t + 1): 
        for j in range(t):
            if S[i + j] != T[j] and S[i + j] != '?':
                break
            if j == t - 1:
                c = S[:i] + T + S[t + i:]
                ans = c.replace('?', 'a')

print(ans)

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

if len(s) < len(t):
	print("UNRESTORABLE")
else:
	ikeru = False
	for i in range(len(s) - len(t), -1, -1):
		num_ok = 0
		for j in range(len(t)):
			if s[i + j] == "?" or s[i + j] == t[j]:
				num_ok += 1
		if num_ok == len(t):
			ikeru = True
			break

	if ikeru:
		for k in range(len(s)):
			if i <= k < i + len(t):
				s[k] = t[k - i]
			elif s[k] == "?":
				s[k] = "a"
		print("".join(s))
	else:
		print("UNRESTORABLE")

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

print(ans)

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

print(ans)

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

ans = 'UNRESTORABLE'
memo = -1

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] == '?':
            cnt += 1
        else:
            break
        if cnt == len(T):
            ans = T
            memo = i


if ans == 'UNRESTORABLE':
    print(ans)
else:
    ans = ''
    ans = S.replace('?','a')
    print(ans[:memo]+T+ans[memo+len(T):])

Solution (Python):
from itertools import zip_longest

S = input()
T = input()

N = len(S)
Tn = len(T)

ans = []
for i in range(N-Tn+1):
    K = S[i:i+Tn]

    for k, t in zip_longest(K, T, fillvalue=""):
        if (k != "?") and (k != t):
            break
    else:
        tmp = S[:i] + T + S[i+Tn:]
        ans.append(tmp.replace("?", "a"))

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

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

ans = "UNRESTORABLE"

memo = -1

for i in range(len(s) - len(t) + 1):
    count = 0
    for j in range(len(t)):
        if s[i+j] == t[j] or s[i+j] == "?":
            count += 1
        else:
            break
        if count == len(t):
            ans = t
            memo = i

if ans == "UNRESTORABLE":
    print(ans)
else:
    ans1 = ""
    for word in s:
        if word == "?":
            ans1 += "a"
        else:
            ans1 += word
    ans2 = ans1[0:memo] + ans + ans1[memo+len(t):]
    print(ans2)

Solution (Python):

S_ = input()
T = input()

S = []
for i in range(0, len(S_) - len(T) + 1):
    tmp = S_[:]
    tmp = tmp[:i] + T + tmp[i + len(T):]

    double = [_ if _ == S_[i] else '?' for i, _ in enumerate(tmp)]
    if ''.join(double) == S_:
        S = tmp

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

Solution (Python):
S=input()
T=input()
answer='UNRESTORABLE'

if len(T)>len(S):
    print(answer)
elif len(T)==len(S):
    for i in range(len(S)):
        if S[i]!='?' and S[i]!=T[i]:
            print(answer)
            break
    print(T)
else:
    for i in range(len(S)-len(T),0,-1):
        tmpS=S[i:i+len(T)]
        flag=True
        for j in range(len(T)):
            if T[j]==tmpS[j] or tmpS[j]=='?':
                continue
            else:
                flag=False
        if flag:
            ans=S[0:i]+T+S[i+len(T):len(S)]
            answer = ans.replace("?","a")
            break

    print(answer)

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

Solution (Python):
S_ = input()
T = input()
a = S_
b = T
def fit(pat, word):
    return all([pi==wi for pi, wi in zip(pat, word) if pi != '?'])

ret = False
for i in reversed(range(len(a)-len(b)+1)):
    if fit(a[i:i+len(b)], b):
        ret = a[:i]+b+a[i+len(b):]
        break    
    
if not ret:
    print('UNRESTORABLE')
elif not '?' in a:
    print(a)
else:
    print(ret.replace('?', 'a'))

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

ans = []
for i in range(len(s)-len(t)+1):
    if s[i] == '?' or s[i] == t[0]:
        ok = True
        j = 1
        while j < len(t) and ok:
            if s[i+j] == '?' or s[i+j] == t[j]:
                j += 1
                continue
            else:
                ok = False
        if ok:
            f = [x for x in s]
            for j in range(len(t)):
                f[i+j] = t[j]
            for j in range(i+1,len(s)):
                if f[j] == '?':
                    f[j] = 'a'
            f = "".join(f)
            ans.append(f)
    if s[i] == '?':
        s[i] = 'a'

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

Solution (Python):
def c_dubious_document_2(S, T):
    
    
    tmp = ''  
    for i in range(len(S) - len(T) + 1):
        
        for j in range(len(T)):
            if S[i + j] == '?' or S[i + j] == T[j]:
                
                continue
            break  
        else:
            
            tmp = S[:i] + T + S[i + len(T):]
    
    ans = tmp.replace('?', 'a') if tmp else 'UNRESTORABLE'
    return ans

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

Solution (Python):
S = str(input())
T = str(input())
a = len(S)
b = len(T)
candidates =[]
if (a < b):
    B = "UNRESTORABLE"
else:
    m = a-b
    B = "UNRESTORABLE"
    i = m
    for i in range(m+1):
        for c1, c2 in zip(S[i:i+b], T):
            if c1 != '?' and c1 != c2:
                break   
        else:
            cand = S[:i] + T + S[i+len(T):]
            candidates.append(cand.replace('?','a'))

if candidates:
    print(min(candidates))
else:
    print('UNRESTORABLE')

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

candidates = []
for i in range(len(S) - len(T) + 1):
    for c1,c2 in zip(S[i:i+len(T)], T):
        if c1 != '?' and c1 != c2:
            break
    else:
        cand = S[:i] + T + S[i+len(T):]
        candidates.append(cand.replace('?','a'))

if candidates:
    print(min(candidates))
else:
    print('UNRESTORABLE')

Solution (Python):
def makeans(s, t, n):
	ans = ''
	if n == -1: return 'UNRESTORABLE'
	for i in range(0, len(s)):
		if n <= i and i <= n+len(t)-1:
			ans = ans + t[i-n]
		else:
			if s[i] == '?':
				ans = ans + 'a'
			else:
				ans = ans + s[i]
	return ans

def find(s, t):
	for i in range(len(t), len(s)+1):
		flag = True
		for j in range(0, len(t)):
			if s[-i+j] != t[j] and s[-i+j] != '?':
				flag = False
				break
		if flag:
			return len(s) - i
	return -1

S = raw_input()
T = raw_input()

print makeans(S, T, find(S, T))

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

import sys


code=list(S)
code.reverse()
S=list(S)
S.reverse()
T=list(T)
T.reverse()


for i in range(len(S)-len(T)+1):
    count=0
    for j in range (len(T)):
        if S[i+j]==T[j] or S[i+j]=='?':
            code[i+j]=T[j]
            count+=1
            if count==len(T):
                for k in range (len(S)):
                    if code[k]=='?':
                        code[k]='a'
                code.reverse()
                print(''.join(code))
                sys.exit()
            
        else:
            code=list(S)
        
print ('UNRESTORABLE')

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


s = input()
t = input()

res = ""
for i in reversed(range(len(s) - len(t)+1)):
    ss = s[i:i+len(t)]
    if judge(ss, t):
        res = s[:i] + t + s[i+len(t):]
        break
print(res.replace("?", "a") if res != "" else "UNRESTORABLE")

Solution (Python):
s = list(input())
t = list(input())
def f():
  for i in range(len(s)-len(t), -1, -1):
    temp = list(s[i:i+len(t)])
    for j in range(len(t)-1, -1, -1):
      if temp[j] == t[j] or temp[j] == '?':
        temp[j] = t[j]
    if temp == t:
      return ''.join(s[:i] + temp + s[i+len(temp):]).replace('?', 'a')
  
  return 'UNRESTORABLE'

print(f())

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


def is_T(s_i, T_i):
    if len(T_i) == 1:
        if s_i[0] == T_i[0] or s_i[0] == '?':
            return True
    
    if s_i[0] == T_i[0] or s_i[0] == '?':
        flag = is_T(s_i[1:], T_i[1:])
        
        if flag:
            return True
    
    return False

for i in range(len(s)-len(T)+1):
    result = is_T(s[i:], T)
    if result:
        ans = ''
        for j in range(i):
            if s[j] != '?':
                ans += s[j]
            else:
                ans += 'a'
        ans += T
        for j in range(len(ans), len(s)):
            if s[j] != '?':
                ans += s[j]
            else:
                ans += 'a'
        print(ans[::-1])
        break
else:
    print('UNRESTORABLE')

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

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


for i in range(len(s))[::-1]:
	ss = s[i:i+len(t)]
	if len(ss) != len(t):
		continue
	if not valid(ss,t):
		continue

	ans = s[0:i] + t + s[i+len(t):]
	ansans = ""
	for c in ans:
		if c == '?':
			ansans += 'a'
		else:
			ansans += c
	print(ansans)
	exit(0)

print("UNRESTORABLE")

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

candidates = []
for i in range(len(S) - len(T) + 1):
    for c1,c2 in zip(S[i:i+len(T)], T):
        if c1 != '?' and c1 != c2:
            break
    else:
        cand = S[:i] + T + S[i+len(T):]
        candidates.append(cand.replace('?','a'))

if candidates:
    print(min(candidates))
else:
    print('UNRESTORABLE')

Solution (Python):
s=list(input())
t=list(input())
sl=len(s)
tl=len(t)
for k in range(sl-tl,-1,-1):
    judge=True
    for i in range(tl):
        if s[i+k]==t[i] or s[i+k]=="?":
            pass
        else:
            judge=False
            break
    if judge:
        for i in range(tl):
            s[i+k]=t[i]
        for i in range(sl):
            if s[i]=="?":
                s[i]="a"
        print("".join(s))
        exit()
print("UNRESTORABLE")

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

S_ans = S
N = len(S)
RESTORABLE = False


for i in range(N):
    isrest = 0
    for j in range(1,len(T)+1):
        if S[N-i-j] != T[-j] and S[N-i-j] != "?":
            isrest += 1
            break
        elif i+j>N:
            isrest += 1
            break
        else:
            temp = list(S_ans)
            temp[N-i-j] = T[-j]
            S_ans = "".join(temp)
    if isrest==0:
        RESTORABLE = True
        break
    S_ans = S
    
if RESTORABLE:
    print(S_ans.replace("?","a"))
else:
    print("UNRESTORABLE")

Solution (Python):
NOT_FOUND = 'UNRESTORABLE'


def m():
    S = list(str(input()))
    T = list(str(input()))
    first = 0
    end = 0
    for i in range(len(S) + 1 - len(T)):
        for k in range(len(T)):
            if S[i + k] != T[k] and S[i + k] != '?':
                break
            if k == len(T) - 1:
                first = i
                end = i + k
    if (end - first) < len(T) - 1:
        return NOT_FOUND

    j = 0
    while first <= end:
        S[first] = T[j]
        first += 1
        j += 1
    ans = "".join(S)
    r = ans.replace('?', 'a')
    return r


print(m())

Solution (Python):


s = input()
t = input()

def isMatch(idx):
    si = idx
    ti = 0
    while si<len(s) and ti<len(t):
        if s[si]==t[ti]:
            si += 1
            ti += 1
            continue
        if s[si]=="?":
            si += 1
            ti += 1
            continue
        if s[si]!=t[ti]:
            return False
    return ti==len(t)

ret = None
for i in range(len(s)-1,-1,-1):
    if isMatch(i):
        ret = s[:i]+t+s[i+len(t):]
        break

if ret is None:
    print("UNRESTORABLE")
else:
    print(ret.replace("?", "a"))

Solution (Python):
s_d = [i for i in input()]
t = input()
flg = True

for i in range(len(s_d)-1, -1, -1):
    if len(s_d) - i >= len(t) and s_d[i] == '?' or s_d[i] == t[0]:
        j, n = i+1, 1
        while j <= len(s_d) - 1 and n <= len(t) - 1:
            if s_d[j] == '?' or s_d[j] == t[n]:
                j += 1
                n += 1
            else:
                break

        if n >= len(t):
            s_d[i:i+len(t)] = t
            break
else:
    print("UNRESTORABLE")
    flg = False

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

    print("".join(s_d))

Solution (Python):
sd = input()
t = input()
keylist = []
for i in range(len(sd) - len(t) + 1):
    a = sd[i:i+len(t)]
    flag = True
    for j in range(len(t)):
        if a[j] == t[j]:
            pass
        elif a[j] == '?':
            a = a[:j] + t[j] + a[j+1:]
            
        else:
            flag = False
            
            break
    if(flag == True):
        keylist.append((sd[:i]+a+sd[i+len(t):]).replace('?','a'))
keylist.sort()
if keylist != []:
    print(keylist[0])
else:
    print('UNRESTORABLE')

Solution (Python):
def compute(s, t):
    for si in reversed(range(len(s)-len(t)+1)):
        for ti in range(len(t)):
            if not (s[si+ti]==t[ti] or s[si+ti]=="?"): break
            if ti!=len(t)-1: continue
            head=s[:si]
            tail=s[si+len(t):]
            return head+t+tail
    return None

s=input()
t=input()
template=compute(s, t)
if template:
    result=""
    for c in template:
        result+="a" if c=="?" else c
    print(result)
else:
    print("UNRESTORABLE")

Solution (Python):
import collections

s = ""
t = ""
res = []


def solve():
    global s,t
    s = input()
    t = input()
    i = 0
    while i+len(t) <= len(s):
        for a,b in zip(t,s[i:]):
            if b != '?' and a != b:
                break
        else:
            x = s[0:i].replace('?','a')+t+s[i+len(t):].replace('?','a')
            res.append(x)
        i+=1
    if len(res):
        print(min(res))
    else:
        print("UNRESTORABLE")



if __name__ == "__main__":
    solve()

Solution (Python):
S = input()[::-1]
T = input()[::-1]

for k in range(1+len(S)-len(T)):
    f = len(T)
    for j in range(len(T)):
        if S[k+j]==T[j] or S[k+j]=="?":
            f-=1
    if f==0:
        print((S[:k]+T+S[k+len(T):]).replace("?","a")[::-1])
        exit(0)

print("UNRESTORABLE")

Solution (Python):
def padding(S, T, i):
    ans = S[:i] + T + S[i+len(X):]
    return ans.replace("?","a")

if __name__ == '__main__':
    
    S = input()
    T = input()

    if len(S) < len(T):
        print("UNRESTORABLE")
        exit(0)

    sizeX = len(T)
    ans = []
    for i in range(len(S) - len(T) + 1):
        X = S[i:i+sizeX]
        for j in range(len(T)):
            if X[j] == "?" or X[j] == T[j]:
                if j == sizeX - 1:
                    ans.append(padding(S, T, i))
            else:
                break

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

Solution (Python):
S = input()
T = input()
lenS,lenT = len(S),len(T)
n,Ans = 10000,"UNRESTORABLE"
if lenS >= lenT:
    for i in range(lenS-lenT,-1,-1):
        check = True
        for j in range(lenT):
            if S[i+j] != "?" and S[i+j] != T[j]:
                check = False
                break
        if check:
            n = i
            break
    if n < 10000:
        Ans = ""
        for i in range(lenS):
            if i >= n and i < n+lenT:
                Ans += T[i-n]
            elif S[i] == "?":
                Ans += "a"
            else:
                Ans += S[i]
print(Ans)

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

slen=len(S)
hlen=len(H)

jisho=[]
ans=False
for i in range(slen-hlen+1):
  jud=True
  for j in range(hlen):
    if S[i+j]!='?' and S[i+j]!=H[j]:
      jud=False
      break
  if jud:
    ansS=S[:i]+H+S[i+hlen:]
    ans=True
    ansS=ansS.replace('?','a')
    jisho.append(ansS)

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

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

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

for i in range(sl-tl,-1,-1):
    j=0
    while j<tl :
        if s[j+i]==t[j] or s[j+i]=='?':
            j+=1
        else:
            break
    if j==tl:
        for k in range(tl):
            s[i+k]=t[k]
        for k in range(sl):
            if s[k]=='?':
                s[k]='a'
        print(''.join(s))
        exit()
print('UNRESTORABLE')

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((s[:i]+t+s[u+i:]).replace("?","a"))
        break
else:
    print("UNRESTORABLE")

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

def check(a, b):
    res = True
    for i in range(len(a)):
        if a[i] != '?' and a[i] != b[i]:
            res = False
            break
    return res

flag = False
ind = -1
for i in reversed(range(len(S) - len(T) + 1)):
    if check(S[i:i+len(T)], T):
        ind = i
        flag = True
        break


if flag:
    ans = ""
    for i in range(len(S)):
        if i >= ind and i < ind + len(T):
            ans += T[i - ind]
        else:
            if S[i] == '?':
                ans += "a"
            else:
                ans += S[i]
    print(ans)
else:
    print(unrest)

Solution (Python):
s = input()
t = input()
u = None
for i in range(len(s) - len(t) + 1):
    ok = True
    for j in range(len(t)):
        if s[i + j] != t[j] and s[i + j] != '?':
            ok = False
            break
    if ok:
        v = []
        for j in range(i):
            if s[j] == '?': v.append('a')
            else: v.append(s[j])
        for j in t:
            v.append(j)
        for j in range(i + len(t), len(s)):
            if s[j] == '?': v.append('a')
            else: v.append(s[j])
        v = ''.join(v)
        if u is None or v < u: u = v
if u is None: print('UNRESTORABLE')
else: print(u)

Solution (Python):
def match(x,y):
    if x==y or y=='?':
        return 1
    else:
        return 0
S=input()
T=input()
U=""

candidate=[]
count=0

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if match(T[j],S[i+j]):
            count+=1
        else:
            count=0
            break
    if count==len(T):
        count=0
        U=S[:i]+T+S[i+len(T):]
        U=U.replace('?','a')
        candidate.append(U)
        
if len(candidate)==0:
    print('UNRESTORABLE')
else:
    candidate.sort()
    print(candidate[0])

Solution (Python):
def match(x,y):
    if x==y or y=='?':
        return 1
    else:
        return 0
S=input()
T=input()
U=""

candidate=[]
count=0

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if match(T[j],S[i+j]):
            count+=1
        else:
            count=0
            break
    if count==len(T):
        count=0
        U=S[:i]+T+S[i+len(T):]
        U=U.replace('?','a')
        candidate.append(U)
        
if len(candidate)==0:
    print('UNRESTORABLE')
else:
    
    print(sorted(candidate)[0])

Solution (Python):
s = list(str(input()))
t = list(str(input()))
ans = []
an = 0
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()
for i in range(len(s)-len(t)+1):
    cou = s[i:i+len(t)]
    for k in range(len(t)):
        if s[i+k] != t[k] and s[i+k] != "?" and t[k] != "?":
            break
        if k == len(t)-1:
            ans.append(s[:i]+t+s[i+len(t):])
for h in range(len(ans)):
    for f in range(len(s)):
        if ans[h][f] == "?":ans[h][f] = "a"
    ans[h] = "".join(ans[h])
if ans == []:
    print("UNRESTORABLE")
    exit()
ans.sort()
print(ans[0])

Solution (Python):
s = list(input())
t = input()
ans = False
for i in range(len(s)-len(t), -1, -1):
  temp = s[i:i+len(t)]
  count = 0
  for j in range(len(temp)):
    if temp[j] == t[j] or temp[j] == "?":
      count += 1
    else:
      break
  if count == len(t):
    ans = True
    s[i:i+len(t)] = t
    s = "".join(s).replace('?', 'a')
    print(s)
    break

if not ans:
  print('UNRESTORABLE')

Solution (Python):
s = input()
t = input()
s,t = s[::-1],t[::-1]
if len(s)<len(t):
	print("UNRESTORABLE")
else:
	for i in range(len(s)-len(t)+1):
		if all(s[i+j]=="?" or s[i+j]==t[j] for j in range(len(t))):
			s = s[:i]+t+s[(i+len(t)):]
			break
	if t not in s:
		print("UNRESTORABLE")
	else:
		answer = s.replace("?","a")[::-1]
		print(answer)

Solution (Python):
s=input()
t=input()
n=len(s)-len(t)+1
lst=[]
for i in range(n):
    tot=0
    ss=s
    for j in range(len(t)):
        if ss[i+j]==t[j] or ss[i+j]=="?":
            tot+=1
    if tot==len(t):
        ss=list(ss)
        t=list(t)
        for j in range(len(t)):
            ss[i+j]=t[j]
        ss="".join(ss)
        t="".join(t)
        ss=ss.replace("?","a")
        lst.append(ss)
lst.sort()
print(lst[0] if len(lst)!=0 else "UNRESTORABLE" )

Solution (Python):
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
t=input()
y=[]
import re
for i in range(len(s)-len(t)+1):
 x=s[:i]
 r=0
 for j in range(len(t)):
   if s[i+j]!="?" and s[i+j]!=t[j]:
      r=1
   else:
      x+=t[j]
 if r==0:
  if i+len(t)<=len(s)-1:
   x+=s[i+len(t):]
       
  y.append(x.replace("?","a"))
y.sort()
if len(y):
 print(y[0])
else:
 print("UNRESTORABLE")

Solution (Python):
import sys
s = input()
t = input()
if(len(s)>=len(t)):
    f = len(s)-len(t)
    e = len(s)
    while(0<=f):
        st = s[f:e]
        flag = True
        for i in range(len(t)):
            if(st[i] != t[i] and st[i] != "?"):
                flag = False
        if(flag):
            ss = s[0:f]+t+(s[e:] if(e<len(s)) else "")
            print(ss.replace("?","a"))
            sys.exit(0)
        f -= 1
        e -= 1
print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
cand = []
if len(s) < len(t):
  print('UNRESTORABLE')
else:
  for i in range(len(s)-len(t)+1):
    if s[i] != t[0] and s[i] != '?':
      continue
    else:
      for j in range(len(t)):
        if s[i+j] != t[j] and s[i+j] != '?':
          break
        elif j == (len(t)-1):
          cand.append(i)
  if len(cand) > 0:
    result = max(cand)
    S = s[:result]+t+s[result+len(t):]
    S = S.replace('?','a')
    print(S)
  else:
    print('UNRESTORABLE')

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

cnt = 0
for i in range(len(S))[::-1]:
    is_ok = True
    for j in range(len(T)):
        if i + j >= len(S):
            is_ok = False
            break
        if S[i + j] != '?' and S[i + j] != T[j]:
            is_ok = False
            break
    if is_ok:
        S = S.replace('?', 'a')
        print(S[:i] + T + S[i + len(T):])
        exit()
print('UNRESTORABLE')

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

lenS = len(S_prime)
lenT = len(T)

for i in range(lenS-lenT,-1,-1):
    m = 0
    for j in range(lenT):
        if T[j] != S_prime[i+j] and S_prime[i+j] != "?":
            break
        m += 1
    if m == lenT:
        S_ans = S_prime[:i] + T + S_prime[i+lenT:]
        S_ans = S_ans.replace("?","a")
        print(S_ans)
        exit()
print("UNRESTORABLE")

Solution (Python):

import fnmatch,sys

s=raw_input()
t=raw_input()
for i in xrange(len(s)-len(t),-1,-1):
 if fnmatch.fnmatch(t,s[i:i+len(t)]):
  res=s[:i]+t+s[i+len(t):]
  res=res.replace('?','a')
  print res
  sys.exit()
print 'UNRESTORABLE'

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

def match(s1, s2):
    if len(s1) < len(s2):
        return False
    for i, j in zip(s1, s2):
        if i != j and i != "?":
            return False
    return True

result = []
for i in range(50):
    if match(s[i:], t):
        result.append((s[:i] + t + s[i + len(t):]).replace("?", "a"))
result.sort()
if not result:
    print("UNRESTORABLE")
else:
    print(result[0])

Solution (Python):
def main():
    s = input()
    t = input()
    for i in reversed(range(len(s)-len(t)+1)):
        for j, c in enumerate(s[i:i+len(t)]):
            if c!='?' and c!=t[j]:
                break
        else:
            ans = s[:i] + t + s[i+len(t):]
            print(ans.replace('?', 'a'))
            return
    print('UNRESTORABLE')

if __name__=='__main__':
    main()

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]


def dim2input(N):
    li = []
    for _ in range(N):
        li.append(input().split())
    return li



dp = None

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

    is_restorable = False
    replace_start_idx = None
    replace_end_idx = None

    for start_idx in range(len(Sp) - len(T) + 1):
        target_idx = 0
        while target_idx < len(T):
            if T[target_idx] == Sp[start_idx + target_idx] or Sp[start_idx + target_idx] == "?":
                target_idx += 1

                if target_idx == len(T):
                    is_restorable = True
                    replace_start_idx = start_idx
                    replace_end_idx = start_idx + target_idx
            else:
                break

    if not is_restorable:
        print("UNRESTORABLE")
    else:
        idx = 0
        while idx < len(Sp):
            if replace_start_idx <= idx < replace_end_idx:
                for t in T:
                    print(t, end="")
                idx += len(T)
            elif Sp[idx] == "?":
                print("a", end="")
                idx += 1
            else:
                print(Sp[idx], end="")
                idx += 1


main()

Solution (Python):
import sys
s=input()
t=input()
u=s[::-1]
v=t[::-1]
for i in range(len(u)-len(v)+1):
  count=0
  outp=u[0:i]
  for j in range(len(v)):
    w=v[j]
    x=u[i+j]
    if(w==x or x=="?"):
      outp+=w
      count+=1
    if(count==len(v)):
      print(((outp+u[i+len(v):])[::-1]).replace("?","a"))
      sys.exit()
print("UNRESTORABLE")