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):
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
INF = float("inf")


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

    ans_list = []

    for i in range(len(S) - len(T) + 1):
        ok = True
        s = list(S[:])
        for j in range(len(T)):
            if S[i + j] == "?" or S[i + j] == T[j]:
                s[i + j] = T[j]
            else:
                ok = False
        if ok:
            ans_list.append("".join(s).replace("?", "a"))

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


if __name__ == '__main__':
    main()

Solution (Python):
s = raw_input()
t = raw_input()
sl = len(s)
tl = len(t)
p = -1
for i in range(sl-tl, -1, -1):
    for j in range(0, tl):
        if s[i+j] != '?' and s[i+j] != t[j]:
            break
    else:
        p = i
        break
if p == -1:
    print 'UNRESTORABLE'
else:
    u = s[0:p] + t + s[p+tl:]
    print u.replace('?', 'a')

Solution (Python):
s = input()[::-1]
t = input()[::-1]
ans = "UNRESTORABLE"

def search():
    si = ti = 0
    for k in range(len(s)-len(t)+1):
        si = k
        ans = s[:k]
        while(True):
            if s[si]==t[ti] or s[si]=='?':
                si+=1
                ti+=1
                ans += t[ti-1]
                if ti == len(t):
                    ans+=s[k+ti:]
                    ans =ans.replace('?','a')
                    return ans[::-1]
                continue
            ti = 0
            break

    return "UNRESTORABLE"

print(search())

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

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

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

found = []
for i in range(N-M+1):
    for j in range(M):
        c1 = S[i+j]
        c2 = T[j]
        if c1 != '?' and c1 != c2: break
    else:
        found.append(i)

if len(found) == 0:
    print('UNRESTORABLE')
    sys.exit()

anss = []
for f in found:
    ans = []
    for i in range(N):
        if f <= i < f + M:
            ans.append(T[i - f])
        elif S[i] == '?':
            ans.append('a')
        else:
            ans.append(S[i])
    anss.append(''.join(ans))

print(min(anss))

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

def possible(S, T):
  for i in range(len(S) - len(T) + 1):
    s = S[i:i + len(T)]
    ok = True
    for j in range(len(T)):
      if s[j] != '?' and s[j] != T[j]:
        ok = False
    if ok:
      return True
  return False  

for i in range(len(S)):
  if S[i] == '?':
    found = False
    for c in 'abcdefghijklmnopqrstuvwxyz':
      s = S[:i] + c + S[i+1:]
      if possible(s, T):
        found = True
        S = s
        break
    if not found :
      print('UNRESTORABLE')
      break
else:
  print(S)

Solution (Python):
no_ans = 'UNRESTORABLE'
a_z = 'abcdefghijklmnopqrstuvwxyz'


def brute_force(s, t, i):
    if i == len(s):
        strs = ''.join(s)
        if strs.find(t) > -1:
            return strs
        else:
            return no_ans

    if s[i] != '?':
        return brute_force(s, t, i + 1)

    for j in range(len(a_z)):
        s[i] = a_z[j]
        ans = brute_force(s, t, i + 1)
        if ans != no_ans:
            return ans
    s[i] = '?'
    return no_ans


def algo(s, t):
    start = len(s) - len(t)
    for i in range(len(s) - len(t), -1, -1):
        ok = True
        for j in range(len(t)):
            ok = s[i + j] == '?' or s[i + j] == t[j]
            if not ok:
                break
        if ok:
            ans = s[:i] + t + s[i + len(t):]
            return ans.replace('?', 'a')
    return no_ans


if __name__ == '__main__':
    s = input()
    t = input()
    
    
    al = algo(s, t)
    print(al)

Solution (Python):





from sys import stdin

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

def solve(S, T):
  minS = '~' * len(S)
  for pos in range(len(S) - len(T) + 1):
    
    if match(S, pos, T):
      S_ = S[:pos] + T + S[pos + len(T):]       
      S_ = [c if c != '?' else 'a' for c in S_] 
      if minS > S_: minS = S_
  return ''.join(minS) if minS[0] != '~' else 'UNRESTORABLE'

S = stdin.readline().strip()
T = stdin.readline().strip()
print(solve(S, T))

Solution (Python):
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

if len(S0)<len(T):
	print "UNRESTORABLE"
	sys.exit()


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	
	for j in range(l):
		
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			
			break
	else:
		
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		
		
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if ans=="":
	print "UNRESTORABLE"
	sys.exit()
else:
	print ans

Solution (Python):


ind = 0
sd = input()
t = input()
len_t = len(t)
len_sd = len(sd)
matched = False

while (len_sd - len_t - ind) >= 0:
    
    partOfSd = sd[len_sd - ind - len_t:len_sd - ind]
    
    
    matched = False
    if len(partOfSd) < len_t:
        break
    else:
        ind_posd = 0
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            
            matched = True
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        
        
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[len_sd - ind - len_t] = t[ind_t]
            ind -= 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

Solution (Python):
def solve():
    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[0:i] + T + S[i+len(T):]
        ans = ans.replace("?", "a")
        print(ans)
 
solve()

Solution (Python):
s_=input()
t=input()
ans=[]
n=len(s_)
m=len(t)
ind=0
dic={}
if m > n:
	print('UNRESTORABLE')
else:
	for i in range(n-m+1):
		con1=True
		for j in range(m):
			if s_[i+j] !=t[j] and s_[i+j]!='?':
				con1=False
				dic.clear()
				break
			else:
				dic[i+j]=t[j]
	
		if con1:
			lis=list(s_)
			if len(dic)!=0:
				for key in dic:
					lis[key]=dic[key]
				lis=''.join(lis)
			lis=lis.replace('?','a')
			ans.append(lis)
			dic.clear()
	if len(ans)!=0:
		ans.sort()
		print(ans[0])
	else:
		print('UNRESTORABLE')

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

def check1(ss):
    asdf = ''
    for i in range(len(t)):
        if i >= len(ss): return False
        if ss[i] != '?' and t[i] != ss[i]: return False
    return True

def lol(ss):
    return ''.join('a' if i == '?' else i for i in ss)
asdf =[]
for i in range(len(s)):
    if check1(s[i:]):
        ss = s[:i] + t + s[i + len(t):]
        asdf += [lol(ss)]
if asdf:
    print(asdf[-1])
else:
    print('UNRESTORABLE')

Solution (Python):
S=[l for l in raw_input()]
T=[l for l in raw_input()]
import copy
result = []
for i in range(len(S)-len(T)+1):
	S_ = copy.copy(S)
	flg = True
	for j in range(len(T)):
		if S[i+j]==T[j]:
			S_[i+j]=T[j]
		elif S[i+j]=='?':
			S_[i+j]=T[j]
		elif S[i+j] != T[j]:
			flg = False
			break
	if flg:
		S_=('').join(S_)
		while '?' in S_:
			S_=S_.replace('?','a')
		result.append(S_)

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

Solution (Python):
def solve():
    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[0:i] + T + S[i+len(T):]
        ans = ans.replace("?", "a")
        print(ans)

solve()

Solution (Python):

a = list(input())
b = list(input())
a_l = len(a)
b_l = len(b)
f = False
for i in range(1,a_l-b_l+2):
    f = True
    for j in range(1,b_l+1):
        if not(b[b_l-j] == a[a_l-i-j+1] or a[a_l-i-j+1] == "?"):
            f = False
            break
    if f:
        for j in range(1,b_l+1):
            a[a_l-i-j+1] = b[b_l-j]
        break
 
if f:
    print("".join(a).replace("?", "a"))
else:
    print("UNRESTORABLE")

Solution (Python):
S_raw = input()
T_raw = input()

S = list(S_raw)
T = list(T_raw)

i = 0
j = 0

j_list = []
S_list = []

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

while True:
	if (j >= ls) or (i >= lt):
		if i < lt  :
			if j_list == []:
				print("UNRESTORABLE")
				exit(0)
			else:
				break
		elif j < ls:
			j_list.append(j)
			i = 0
			j = j - lt + 1

		else:
			j_list.append(j)
			break

	if T[i] == S[j]:
		i += 1
		j += 1
	elif S[j] == '?':
		i += 1
		j += 1
	else:
		j -= (i - 1)
		i = 0

for k in j_list:
	M = S[:]
	for i,c in zip(range(k-lt,k),T):
		M[i] = c

	S_tmp = ''

	for i in range(ls):
		if M[i] == '?':
			S_tmp += 'a'
		else:
			S_tmp += M[i]

	S_list.append(S_tmp)

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

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):
def C_Dubious_Document_2(S, T):
    """
    S:鍵の文字列のいくつかを'?'で置き換えたもの
    T:Sの中に含まれている可能性のある文字列
    """
    ans = 'UNRESTORABLE'
    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
            if j == len(T) - 1:
                
                
                tmp = S[:i] + T + S[i + len(T):]
                
                tmp = tmp.replace('?', 'a')
                ans = tmp
    return ans

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

Solution (Python):
a = raw_input()
b = raw_input()
a = list(a)
b = list(b)
n = len(a)
m = len(b)
z = ''
for i in range(n - m + 1):
	flag = True
	for j in range(m):
		if a[i + j] != b[j] and a[i + j] != '?':
			flag = False
	if not flag:
		continue
	c = a[::]
	for j in range(m):
		c[i + j] = b[j]
	for i in range(n):
		if c[i] == '?':
			c[i] = 'a'
	if z == '':
		z = c
	else:
		z = min(z, c)
if z == '':
	print 'UNRESTORABLE'
else:
	print ''.join(z)

Solution (Python):
S = raw_input()
T = raw_input()
ans = "z"*100
for i in range(len(S)-len(T)+1)[::-1]:
    ok = True
    for j in range(len(T)):
        if S[i+j]!="?" and S[i+j]!=T[j]:
            ok = False
    if ok:
        s = S[:i]+T+S[i+len(T):]
        ans = min(ans, s.replace("?", "a"))
        break
if ans=="z"*100:
    print "UNRESTORABLE"
else:
    print ans

Solution (Python):
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)
	l=[]
	ans = ""
	f = False
	ff = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            if _s[i] == t:
	                ans += _s[i]
	                ff = True
	            else:
	                ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	    

	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       
	       if ff:
	           l.append(''.join(_ans).replace("X","a"))
	       else:
	           _ans[i:i+len(t)] = t
	           for i in range(len(_ans)):
	               if _s[i] != '?':
	                   _ans[i] = _s[i]
	           l.append(''.join(_ans).replace("X","a"))
	       
	if f:
	    ans = ''.join(min(l))
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()

Solution (Python):
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:
    r=list(s)
    r[i:i+len(t)]=t
    for j in range(len(r)):
        if r[j]=='?':
            r[j]='a'
    print(''.join(r))

Solution (Python):



import re

s=input()



s_t=input()
sl=[]
sl.extend(s)

hl=re.compile(r'\?+').findall(s)
cl=re.compile(r'\w+').findall(s)


hf = False
f = True
tl=[]

if sl[0] == '?':
    for i in range(len(hl)-1):
        tl.append(hl[i])
        tl.append(cl[i])
    if len(hl) != len(cl):
        tl.append(hl[len(cl)])
else:
    for i in range(len(cl)-1):
        tl.append(cl[i])
        tl.append(hl[i])
    if len(hl) != len(cl):
        tl.append(cl[len(hl)])
        
        



for t in tl:
    if t.find(s_t) != -1:
        s = s.replace('?','a')
        print(s)
        exit()

def fin(s,i):
    
    t=0
    for c in s:
        if c == i[t] or c == '?':
            t+=1
            continue
        
        else:
            
            return False
    
    return True
    
for w in range(len(s)-len(s_t)+1):
    
    
    if True:
        
        if fin(s[len(s)-w-len(s_t):len(s)-w],s_t):
            s = s[0:len(s)-w-len(s_t)] + s_t +s[len(s)-w:len(s)]
            s = s.replace('?','a')
        
            print(s)
            exit()
    else:
        continue

for t in tl:
    if t[0]!='?':
        continue
    if len(s_t)<= len(t):
        ind=tl.index(t)
        tl[ind]=(len(t)-len(s_t))*'?'+s_t
        ans=''
        for t in tl:
            ans=ans+t
        
        ans = ans.replace('?','a')
        print(ans)
        exit()

print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()
S_ =S
l = []
for i in range(len(S)-len(T)+1):
    h = [S[i+j] == T[j] or S[i] == '?' for j in range(len(T))]
    for j in range(len(T)):
        if S[i+j] != '?' and S[i+j] != T[j]:
            h = False
    if h:
        S_ = S[:i] + T + S[i+len(T):]
        l.append(S_)
    h = True
    
if len(l) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(l)[0].replace('?','a'))

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

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

if N < M:
    jdg = False
else:
    
    jdg = T in S
    
    if jdg==False:
        for i in range( N-M, -1, -1 ):
            jdg = True
            for j in range(M):
                if S[i+j] != T[j] and S[i+j] != '?':
                    jdg = False
                    break;
            if jdg:
                li = list(S)
                li[i:i+M] = T
                S = ''.join(li)
                break;

if jdg:
    ans = S.replace('?', 'a')
    print(ans)
else:
    print("UNRESTORABLE")

Solution (Python):
import sys
 
S = sys.stdin.readline().rstrip()
T = sys.stdin.readline().rstrip()
 
def checkValid(S, i, T):
    try:
        if S[i] == T[0]:
            for j in range(0, len(T)):
                if S[i+j] == T[j] or S[i+j] == '?':
                    pass
                else:
                    return False
        else:
            return False
        return True
    except Exception as e:
        return False
 
def checkValid2(S, i, T):
    try:
        for j in range(0, len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                pass
            else:
                return False
        return True
    except Exception as e:
        return False
def getResult(S, i, T):
    res = ""
    for k in range(0, len(S)):
        if k < i:
            res = res + S[k]
        elif k >= i and k < i+len(T):
            res = res + T[k-i]
        else:
            res = res + S[k]
    S = res.replace('?', 'a')
    return S
 
res_list = []
 
res = False
for i in range(0, len(S)):
    if S[i] != '?':
        if checkValid(S, i, T) == True:
            res_list.append(getResult(S, i, T))
            res = True
    else:
        if checkValid2(S, i, T) == True:
            res_list.append(getResult(S, i, T))
            res = True
 
if res == False:
    print("UNRESTORABLE")
else:
    smallest = res_list[0]
    for i in range(0, len(res_list)):
        if smallest > res_list[i]:
            smallest = res_list[i]
    print (smallest)

Solution (Python):
s = input()
t = input()
ans = "UNRESTORABLE"

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
        if j == len(t) - 1:
            mazai = (s[:i] + t + s[i+len(t):]).replace("?","a")
            if True or mazai < "UNRESTORABLE":
                ans = mazai
print(ans)

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

answers = []

for i in xrange(len(S) - len(T) + 1):
    for j in xrange(len(T)):
        flag = True
        if S[i+j] != '?' and S[i+j] != T[j]:
            flag = False
            break
    if flag:
        answers.append((S[:i] + T + S[i+len(T):]).replace("?", "a"))

if len(answers) == 0:
    print 'UNRESTORABLE'
    exit()

answers.sort()
print answers[0]

Solution (Python):
sd = raw_input()
t = raw_input()
lnsd = len(sd)
lnt = len(t)
sa = 0
tru = None
for i in range(lnsd - lnt + 1):
    for i2 in range(lnt):
        a = sd[(0+sa):(lnt+sa)]
        if a[i2] != t[i2] and a[i2] != "?":
            break
        if i2 == lnt-1:
            tru= i
    sa = sa+1
if tru == None:
    print("UNRESTORABLE")
    exit()
sd = list(sd)
for i in range(lnt):
    sd[tru+i]=t[i]
strn ="".join(sd)
print(strn.replace("?", "a"))

Solution (Python):
def checker(L1, L2):
    N = len(L1)
    for i in range(N):
        if L1[i] == L2[i] or L1[i] == "?":
            pass
        else:
            return False

    return True


def solve():
    while 1:
        try:
            S_prime = list(input())
            T = input()
            if len(T) > len(S_prime):
                print("UNRESTORABLE")
                raise EndLoop
            elif S_prime.count("?") == 0:
                if T in "".join(S_prime):
                    print("".join(S_prime))
                    raise EndLoop
                else:
                    print("UNRESTORABLE")
                    raise EndLoop
            elif S_prime.count("?") == len(S_prime):
                S = "".join(["a" for _ in range(len(S_prime) - len(T))]) + T
                print(S)
                raise EndLoop

            possible_ans = []
            for i in range(len(S_prime) - len(T) + 1):
                tmp_str = list(S_prime[i:len(T) + i])
                if checker(tmp_str, T):
                    if i + len(T) == len(S_prime):
                        S = "".join(S_prime[:i]) + "".join(T)
                        possible_ans.append(S.replace("?", "a"))
                    else:
                        S = "".join(S_prime[:i]) + T + "".join(S_prime[i + len(T):])
                        possible_ans.append(S.replace("?", "a"))
                        
            if len(possible_ans) == 0:
                print("UNRESTORABLE")
            else:
                print(min(possible_ans))
            break
        except:
            break


solve()

Solution (Python):


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

if(len(s)<len(t)):
    flg = False
for i in range(len(s)-len(t)+1)[::-1]:
    tmpstr = s[i:i+len(t)]
    flg = True
    for j in range(len(t)):
        if(tmpstr[j] == '?'):
            pass
        elif(tmpstr[j] == t[j]):
            pass
        else:
            flg = False
    if(flg):
        uset = list(range(i,i+len(t)))
        for k in range(len(s)):
            if(k in uset):
                ansstr += t[k-i]
            elif(s[k]=='?'):
                ansstr += 'a'
            else:
                ansstr += s[k]
        break
print(''.join(ansstr) if flg else 'UNRESTORABLE')

Solution (Python):

S = input()
T = input()
for i in range(len(S)-len(T), -1, -1):
    for cs, ct in zip(S[i:i+len(T)], T):
        if not (cs == "?" or cs == ct):
            break
    else:
        S = S[:i] + T + S[i+len(T):]
        S = S.replace("?", "a")
        print(S)
        break
else:
    print("UNRESTORABLE")

Solution (Python):
import re

Sp = list(input())
T=list(input())
Sp = Sp[::-1]
T = T[::-1]
isOK=0
for n in range(len(Sp)-len(T)+1):
    count=0
    for m in range(len(T)):
        if Sp[n+m] == '?':
          count += 1
        elif Sp[n+m] == T[m]:
          count += 1

    if count == len(T):
        Sp[n:n+len(T)] = T
        isOK=1
        break

Sp = "".join(Sp[::-1])
Sp = Sp.replace("?", "a")

if isOK == 1:
    print(Sp)
else:
    print("UNRESTORABLE")

Solution (Python):
import re

S1 = input()						
T = input()						

S = re.sub(r'\?', '.', S1)

X="UNRESTORABLE"



N = len(S)-len(T)+1

A=-1

for i in range(N):
	s = S[i:(len(T)+i)]
	m = re.match(s, T)
	if m:
		A = i
	



if A==-1:
	ans = X
else:
	ans = S[0:A] + T + S[(A + len(T)):len(S)]
	ans = re.sub(r'\.', 'a', ans)


print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string

S = input()
T = input()

slen = len(S)
tlen = len(T)

def can_fill(substr, T):
    for s, t in zip(substr, T):
        if s != '?' and s != t:
            return False
    return True

ans = None
for i in range(len(S) - tlen + 1):
    substr = S[i:i+tlen]
    if can_fill(substr, T):
        S2 = S[:i] + T + S[i+tlen:]
        S2 = S2.replace('?', 'a')
        if ans == None:
            ans = S2
        else:
            ans = min(ans, S2)

if ans is None:
    print('UNRESTORABLE')
else:
    print(ans)

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

flag = 0
count = 0
new = ''
l = []
for i in range(len(s)):
    if s[i] == '?' or s[i] in t:
        if count == 0:
            start = i
        count += 1
    else:
        if count >= len(t):
            for j in range(count-len(t)+1):
                check = 0
                for k in range(len(t)):
                    if s[start+j+k] != t[k] and s[start+j+k] != '?':
                        check = 1
                        break
                if check == 0:
                    new = s[:start+j] + t + s[start+len(t)+j:]
                    l.append(new)
                    flag = 1
        count = 0

if count >= len(t):
    for j in range(count-len(t)+1):
        check = 0
        for k in range(len(t)):
            if s[start+j+k] != t[k] and s[start+j+k] != '?':
                check = 1
                break
        if check == 0:
            new = s[:start+j] + t + s[start+len(t)+j:]
            l.append(new)
            flag = 1

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

Solution (Python):
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            if len(S)-i < len(T):
                flag = False
                break
            for e in range(i - j):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k - j] == T[k]:
                    continue
                elif Z[i + k - j] == "?":
                    Z = Z.replace("?", T[k], 1)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
        elif Z[i] == "?":
            if len(S)-i < len(T):
                flag = False
                break
            for e in range(i):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k] == T[k]:
                    continue
                elif Z[i + k] == "?":
                    Z = Z.replace("?", T[k], 1)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)

if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Solution (Python):
import numpy as np

S = input()
T = input()

npS =  np.array(list(S))
npT =  np.array(list(T))

must_sym_i = npS != '?'

accepted = []
for i in range(len(S)-len(T)+1):
    and_ = npS[i:i+len(T)] == npT
    if all(and_[must_sym_i[i:i+len(T)]]):
        temp = np.copy(npS)
        temp[i:i+len(T)] = npT
        temp[temp == '?'] = 'a'
        accepted.append(temp)

acc = [''.join(list(accepted[i])) for i in range(len(accepted))]

if len(acc) > 0:
    print(sorted(acc)[0])
else:
    print("UNRESTORABLE")

Solution (Python):
s_=input()
t=input()
 
filled_idx=[i for i in range(len(s_)) if s_[i]!="?"]
 
for t_s in range(len(s_)-len(t),-1,-1):
    t_e=t_s+len(t)
    s=s_[:t_s]+t+s_[t_e:]
    if(all(s[i]==s_[i] for i in filled_idx)):
        s=s.replace("?","a")
        print(s)
        break
else:
    print("UNRESTORABLE")

Solution (Python):
S = input().strip()
T = input().strip()
rS = []
if len(S) >= len(T):
    for i in range(len(S)):
        rS.append(S[i])
    for i in range(len(rS) - len(T), -1, -1):
        eq = True
        for j in range(len(T)):
            if not(rS[i + j] == T[j] or rS[i + j] == "?"):
                eq = not(eq)
                break
        if eq:
            for j in range(len(T)):
                rS[i + j] = T[j]
            break
    if eq:
        for i in range(len(rS)):
            if rS[i] == "?":
                rS[i] = "a"
        for i in range(len(rS)):
            print(rS[i], end='')
        print("")
    else:
        print("UNRESTORABLE")
else:
    print("UNRESTORABLE")

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



def A(S):
    ok=-1
    for i in range(len(S)):
        if len(T)+i>len(S):
            if ok==-1: return 1000
            else: return ok
        if check(i):
            ok=i
    if not ok==-1:
        return ok
    return 1000

def check(i):
    j=i
    for A in T:
        if not(A==S[j] or S[j]=='?'):
            return False
        else: j+=1
    return True


i=A(S)

if i==1000:
    print('UNRESTORABLE')
    
else:
    for j in range (len(T)):
        S[i+j]=T[j]
    for k in range(len(S)):
        if S[k]=='?':
            S[k]='a'
    print(''.join(S))

Solution (Python):

if __name__ == '__main__':
    SS = raw_input()
    T = raw_input()
    S = ""
    start_index = -1
    end_index = -1
    t_index = 0
    state = 0
    if len(T) > len(SS):
        S = 'UNRESTORABLE'
    else:
        for index in range(len(SS) - len(T), -1, -1):
            for current_index in range(index, index + len(T)):
                if SS[current_index] == '?' or SS[current_index] == T[t_index]:
                    t_index += 1
                    if t_index == len(T):
                        start_index = index
                        end_index = current_index
                        state = 1
                        break
                else:
                    t_index = 0
                    break
            if state == 1:
                break
        if start_index == -1:
            S = 'UNRESTORABLE'
        else:
            for index, s in enumerate(SS):
                if index >= start_index and index <= end_index:
                    S += T[index - start_index]
                elif s == '?':
                    S += 'a'
                else:
                    S += s
    print S

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



jdg = True

if N < M:
    print("UNRESTORABLE")
else:
    for i in range( N-M+1 ):
        jdg = True
        for j in range(M):
            if S[i+j] != T[j]:
                jdg = False
                break;
        if jdg:
            break;
    

    if jdg:
         ans = S.replace('?', 'a')
         print(ans)

    
    else:
        for i in range( N-M, -1, -1 ):
            jdg = True
            for j in range(M):
                if S[i+j] != T[j] and S[i+j] != '?':
                    jdg = False
                    break;
            if jdg:
                li = list(S)
                li[i:i+M] = T
                S = ''.join(li)
                break;

        if jdg:
            ans = S.replace('?', 'a')
            print(ans)
        else:
            print("UNRESTORABLE")

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

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

for i in reversed(range(len(s) - len(t)+1)):
    if eq(s[i:i+len(t)], t[:]):
        s[i:i + len(t)] = t[:]
        ans = ''.join(s)
        print(ans.replace('?', 'a'))
        quit()
print('UNRESTORABLE')

Solution (Python):
Sd_ = input()
T_ = input()
Sd = []
T = []
S = []
for i in range(len(Sd_)):
    Sd.append(Sd_[i])
for i in range(len(T_)):
    T.append(T_[i])


def check(s, t, n):
    for i in range(len(t)):
        if s[n + i] is not t[i] and s[n + i] is not '?':
            return False
    return True


m = len(Sd) - len(T)
while m >= 0:
    if check(Sd[:], T[:], m):
        for j in range(len(T)):
            Sd[m + j] = T[j]
        for a in range(len(Sd)):
            if Sd[a] is '?':
                S.append('a')
            else:
                S.append(Sd[a])
        print(''.join(S))
        exit()
    m -= 1
print('UNRESTORABLE')