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

Solution (Python):

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

def check(i,S,T):
    for k in range(len(T)):
        if (S[i+k] == "?" or S[i+k] == T[k]):
            continue
        else:
            return False
    for k in range(len(T)):
        S[i+k] = T[k]
    for j in range(len(S)):
        if S[j] == "?":
            S[j] = "a"
    return S[::-1]

count = 0
for i in range(len(S_rev) - len(T_rev) +1):
    a = check(i,S_rev,T_rev)
    if a:
        a = "".join(a)
        print(a)
        count = 1
        break

if count == 0:
    print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
ls = len(S)
lt = len(T)

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

Solution (Python):
S = input()
T = input()
len_s = len(S)
len_t = len(T)

for i in range(len_s-len_t, -1, -1):
    for j, t in enumerate(T):
        if t == S[i+j] or S[i+j] == "?":
            pass
        else:
            break
    else:
        ans = list(S)
        ans[i:i+len_t] = T
        print("".join(ans).replace("?","a"))
        exit()

print("UNRESTORABLE")

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

for i in range(1,N-M+2):
  count = 0
  for j in range(1,M+1):
    if S[N-i-j+1] == T[M-j] or S[N-i-j+1] == "?":
      count += 1
    else:
      break
  if count == M:
    S = S[:N-i-M+1]+T+S[N-i+1:]
    S = S.replace("?","a")
    print(S)
    exit()
else:
  print("UNRESTORABLE")

Solution (Python):
import sys
from io import StringIO
import unittest
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

import re


def resolve():
    s_prime = sys.stdin.readline().split()[0]
    T = sys.stdin.readline().split()[0]
    logger.debug(s_prime)
    logger.debug(T)

    s_len = len(s_prime)
    t_len = len(T)

    r_s_prime = s_prime[::-1]
    r_t = T[::-1]

    answer = "UNRESTORABLE"
    for i_s, s_char in enumerate(r_s_prime):
        if s_len - i_s >= t_len:
            is_match = True
            for i_t, t_char in enumerate(r_t):
                if r_s_prime[i_s + i_t] == '?' or r_s_prime[i_s +
                                                            i_t] == t_char:
                    pass
                else:
                    is_match = False
                    break

            if is_match:
                answer = r_s_prime[:i_s] + r_t + r_s_prime[i_s + len(r_t):]
                answer = answer[::-1]
                pattern = re.compile(r'\?')
                answer = pattern.sub('a', answer)
                break
        else:
            break

    print(answer)


if __name__ == "__main__":
    resolve()





class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """?tc????
coder"""
        output = """atcoder"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """??p??d??
abc"""
        output = """UNRESTORABLE"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """?
a"""
        output = """a"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """?
ab"""
        output = """UNRESTORABLE"""
        self.assertIO(input, output)

    def test_入力例_5(self):
        input = """??????????
abc"""
        output = """aaaaaaaabc"""
        self.assertIO(input, output)

Solution (Python):



from copy import deepcopy
s = list(input())
t = list(input())

ns = len(s)
nt = len(t)
f1 = 0
for i in range(ns):
    if s[i] in t:
        ind = t.index(s[i])
        for j in range(i-ind,i-ind+nt):
            if j < ns:
                if s[j] == t[j-i+ind] or s[j] == '?':
                    continue
                else:
                    break
            else:
                break
        else:
            f1 = 1
            x = deepcopy(s)
            x[i-ind:i-ind+nt] = t
            break
f2 = 0
for i in range(ns)[::-1]:
    if s[i] == '?':
        for j in range(i,i+nt):
            if j < ns:
                if s[j] == t[j-i] or s[j] == '?':
                    continue
                else:
                    break
            else:
                break
        else:
            f2 = 1
            y = deepcopy(s)
            y[i:i+nt] = t
            break
if f1 & f2:
    for i in range(ns):
        if x[i] == '?':
            x[i] = 'a'
        if y[i] == '?':
            y[i] = 'a'
    x = ''.join(x)
    y = ''.join(y)
    ans = [x,y]
    ans.sort()
    print(ans[0])

elif f1 and not f2:
    for i in range(ns):
        if x[i] == '?':
            x[i] = 'a'
    x = ''.join(x)
    print(x)
elif not f1 and f2:
    for i in range(ns):
        if y[i] == '?':
            y[i] = 'a'
    y = ''.join(y)
    print(y)
else:
    print('UNRESTORABLE')

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

ans = []
for i in range(len(S)-len(T)+1):
    flag = 1
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == "?":
            continue
        else:
            flag = 0
    if flag == 1:
        S_restore = list(S)
        for j in range(len(T)):
            S_restore[i+j] = T[j]
        for j in range(len(S)):
            if S_restore[j] == "?":
                S_restore[j] = "a"
        ans.append("".join(map(str, S_restore)))
ans.sort()

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

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

Solution (Python):

s = input()
T = input()

S = "UNRESTORABLE"

if len(s) >= len(T):
    
    for i in range(len(s) - len(T) + 1):
        s_partial = s[i:i + len(T)]
        
        for j in range(len(T)):
            
            if s_partial[j] not in ("?", T[j]):
                s_partial = None
                break
        if s_partial == None:
            continue
        
        text = s[:i] + T + s[i + len(T):]
        
        text = text.replace("?", "a")
        
        if S == "UNRESTORABLE" or text < S:
            
            S = text

print(S)

Solution (Python):
def solve(S, T):
    for i in range(len(S) - len(T), -1, -1):
        ok = True
        for j in range(len(T)):
            if not (S[i + j] == "?" or S[i + j] == T[j]):
                ok = False
        if ok:
            return S[:i].replace("?", "a") + T + S[i + len(T) :].replace("?", "a")

    return "UNRESTORABLE"


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

Solution (Python):
import re

s = input()
T = input()

pattern = s.replace("?", ".")

N = len(s) - len(T)

S = "UNRESTORABLE"

for i in range(N + 1):
    
    text = pattern[:N - i] + T + pattern[::-1][:i][::-1]
    
    matchOB = re.match(pattern, text)
    
    if matchOB:
        
        text = text.replace(".", "a")
        
        if S == "UNRESTORABLE" or text < S:
            
            S = text

print(S)

Solution (Python):
S=input()
T=input()
lens=len(S)
lent=len(T)
ans=[]
for i in range(lens-lent+1):
    SS=S[i:i+lent]
    for j in range(len(SS)):
        if SS[j]!=T[j] and SS[j]!="?":
            break
    else:
        SSS=S[:i]+T+S[lent+i:]
        ans.append(SSS.replace("?","a"))
ans.sort()
if ans==[]:
    print("UNRESTORABLE")
else:
    print(ans[0])

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

Solution (Python):
S = input()
lens = len(S)
T = input()
lent = len(T)
if lens < lent:
    print('UNRESTORABLE')
else:
    ii = lens - lent
    while True:
        subs = S[ii:]
        for j in range(lent):
            if subs[j] == '?':pass
            elif subs[j] != T[j]:
                
                ii -= 1
                break
        else:
            
            newS = ''
            if ii == 0:newS = T+S[lent:]
            else:newS = S[0:ii] + T + S[ii+lent:]
            print(newS.replace('?','a'))
            break
        if ii < 0:
            print('UNRESTORABLE')
            
            break

Solution (Python):
import sys

input = sys.stdin.readline

S = list(input())
T = list(input())
S.pop()
T.pop()

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

def main():
    store = []
    for i in range(N - M + 1):
        X = S[i:i+M]
        for j in range(M):
            if X[j] != T[j] and X[j] != "?":
                break
        else:
            store.append(i)
        
    if len(store) == 0:
        return 0
    else:
        index = store.pop()
        if index + M == N:
            ans = S[:index] + T
        else:
            ans = S[:index] + T + S[index + M:]

    for i in range(N):
        if ans[i] == "?":
            ans[i] = "a"
    return "".join(ans)

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

Solution (Python):
S_ = input()
T = input()
S = "UNRESTORABLE"
for i, s in enumerate(S_):
    if i + len(T) > len(S_):
        break
    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")
print(S)

Solution (Python):
s = input()
t = input()
ls, lt = map(len, [s, t])
for i in range(lt-1, ls)[::-1]:
    if ls < lt:
        break
    flag = 0
    for j in range(lt):
        if s[i-j] == "?":
            continue
        elif s[i-j] != t[lt-1-j]:
            flag = 1
            break
    if flag == 0:
        s = s.replace("?", "a")
        print(s[:i-lt+1]+t+s[i+1:])
        exit()
print("UNRESTORABLE")

Solution (Python):
s=input()
t=input()
n=len(s)
m=len(t)
s_list=[]
for i in range(n):
    temp=''
    if n-i>=m:
        for j in range(n):
            if i<=j<=i+m-1:
                if s[j]=='?':
                    temp=temp+t[j-i]
                else:
                    if s[j]==t[j-i]:
                      temp=temp+t[j-i]
                    else:
                        break
            else:
                if s[j]=='?':
                    temp=temp+'a'
                else:
                    temp=temp+s[j]
        else:
            s_list.append(temp)
s_list.sort()
if s_list==[]:
    print('UNRESTORABLE')
else:
    print(s_list[0])

Solution (Python):
s = input()
t = input()
ls, lt = map(len, [s, t])
for i in range(lt-1, ls)[::-1]:
    if ls < lt:
        break
    flag = 0
    for j in range(lt):
        if s[i-j] == "?":
            continue
        elif s[i-j] != t[lt-1-j]:
            flag = 1
            break
    if flag == 0:
        s = s.replace("?", "a")
        print(s[:i-lt+1]+t+s[i+1:])
        exit()
print("UNRESTORABLE")

Solution (Python):
from sys import stdin
S = (stdin.readline().rstrip())
T = (stdin.readline().rstrip())

def count_match(a,b):
    cnt = 0
    for w1,w2 in zip(a,b):
        if w1 == w2:
            cnt += 1
    return cnt

flag = False
for i in range(len(S)-len(T)+1):
    match = count_match(S[i:i+len(T)],T) + S[i:i+len(T)].count("?")
    if match == len(T):
      flag = True
      ans = S[:i] + T + S[i+len(T):]
      ans = ans.replace("?","a")
if flag:
  print(ans)
else:
  print("UNRESTORABLE")

Solution (Python):

S = input()
T = input()


_S = []
for s in S:
    _S.append(ord(s)-97)

_T = []
for t in T:
    _T.append(ord(t)-97)


s_len = len(_S)
t_len = len(_T)
flag = False
buff = 0
for i in range(s_len-t_len+1):
    count = 0
    for j in range(t_len):
        if _S[i+j] == _T[j] or _S[i+j] == -34:
            count += 1
        else:
            continue
    if count >= t_len:
        buff = i
        flag = True
        


_buff = []

if not flag:
    print("UNRESTORABLE")
    exit()
else:
    
    for i in range(t_len):
        _S[buff+i] = _T[i]

for i in range(s_len):
    if _S[i] == -34:
        _S[i] = 0

ans = []

for i in range(s_len):
    ans.append(chr(97+_S[i]))

print(''.join(ans))

Solution (Python):
s=input()
t=input()
ls=len(s)
lt=len(t)
S=''
for i in range(ls-lt+1):
    cnt=0
    for j in range(lt):

        if s[i+j]==t[j]:
            cnt+=1
        elif s[i+j]=='?':
            cnt+=1
        else:
            break
    if cnt==lt:
        S=s[:i]+t+s[i+lt:]
if S=='':
    print('UNRESTORABLE')
else:
    S=list(S)
    for i in range(ls):
        if S[i]=='?':
            S[i]='a'
    print(''.join(S))

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

def slv(S, T):
    cnd = []
    for i in range(len(S) - len(T) + 1):
        a = ''
        for j in range(len(T)):
            if S[i+j] in ('?', T[j]):
                a += T[j]
                continue
            break
        if len(a) == len(T):
            for j in range(i-1, -1, -1):
                a = ('a' if S[j] == '?' else S[j]) + a
            for j in range(i + len(T), len(S)):
                a += ('a' if S[j] == '?' else S[j])
            cnd.append(a)
    if len(cnd) == 0:
        return 'UNRESTORABLE'
    cnd.sort()
    return cnd[0]

print(slv(S, T))

Solution (Python):

s = input()
t = input()


S = list(s)
T = list(t)

S.reverse()
T.reverse()


flag = False

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

else:
    
    for i in range(len(S)-len(T)+1):

        for j in range(len(T)):

            fl2 = True

            if S[i+j] != T[j] and S[i+j] != "?":
                fl2 = False
                break

        if fl2:

            for j in range(len(T)):
                
                flag = True
                S[i+j] = T[j]

            break


    if flag:

        ans = ""

        for i in S:

            if i == "?":
                ans = "a" + ans
            else:
                ans = i + ans


        print (ans)
        
    else:

        print ("UNRESTORABLE")

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

Solution (Python):



s=input()
t=input()

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

ans=[]

for i in range(len_s-len_t+1):
    flag=True
   
    for j in range(len_t):
        if s[i+j]==t[j] or s[i+j]=='?':continue
        else:
            flag=False
            break
    if flag:
        ans.append((s[:i]+t[:]+s[len_t+i:]).replace('?','a'))

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

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

Solution (Python):
s = list(input())
len_s = len(s)
t = list(input())
len_t = len(t)

if len_s<len_t:
    print("UNRESTORABLE")
    exit()

def fix(ls):
    for i in range(len(ls)):
        if ls[i]=="?":
            ls[i]="a"
    return ls

ans = []
for i in range(len_s-len_t+1):
    flg = True
    for j in range(len_t):
        if t[j]==s[i+j] or s[i+j]=="?":
            pass
        else:
            flg=False
            break
    if flg:
        can1 = fix(s[0:i])
        can2 = t
        can3 = fix(s[i+len_t:])
        ans.append("".join(can1+can2+can3))
ans.sort()
if len(ans)!=0:
    print(ans[0])
else:
    print("UNRESTORABLE")

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

kainashi="UNRESTORABLE"
ans1=[]
for i in range(len(s)-len(t)+1):
    tmp=s[:]
    flag=1
    for j in range(len(t)):
        if s[len(s)-i-len(t)+j]==t[j] or s[len(s)-i-len(t)+j]=="?":
            tmp[len(s)-i-len(t)+j]=t[j]
        else:
            flag=0
        
    if flag==1:
        ans=""
        for k in range(len(s)):
            if tmp[k]=="?":
                tmp[k]="a"
            ans+=tmp[k]
        ans1.append(ans)

if len(ans1)>0:
    ans1.sort()
    print(ans1[0])
else:
    print(kainashi)

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

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

ans_list = []

for i in range(len(s)-len(t)+1)[::-1]:
    flag=True
    for j in range(len(t)):
        if s[i+j]=="?" or s[i+j]==t[j]:
            continue
        else:
            flag=False
            break
    if flag:
        ans = "".join(s[:i]+t+s[i+len(t):]).replace("?","a")
        ans_list.append(ans)
    else:
        continue
if len(ans_list)==0:
    print('UNRESTORABLE')
else:
    print(min(ans_list))

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


def match(k):
    s = S[k:k+len(T)]
    
    for j in range(len(s)):
        if s[j] not in (T[j], '?'):
            return False

    return True

def construct(k):
    ans = list(S)

    for i in range(len(T)):
        ans[k+i] = T[i]
    
    for i in range(len(S)):
        if (ans[i] == '?'):
            ans[i] = 'a'

    return ''.join(ans)

res = []
for i in range(len(S) - len(T) + 1):
    if match(i):
        s = construct(i)
        res.append(s)

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

Solution (Python):
import sys

S_ = list(sys.stdin.readline()[:-1])
T = list(sys.stdin.readline()[:-1])

start_right = -1
for i in range(len(S_)):
    if (S_[i] == "?") or (S_[i] == T[0]):
        for j in range(1, len(T)):
            if (i+j >= len(S_)):
                break
            if (S_[i+j] == "?") or (S_[i+j] == T[j]):
                continue
            else:
                break
        else:
            start_right = i
    
for i in range(len(T)):
    S_[start_right + i] = T[i]

if (start_right != -1):
    for s in S_:
        print(s if (s != "?") else "a", end="")
    print()
else:
    print("UNRESTORABLE")

Solution (Python):
n=input()
m=input()
le=len(m)
check=0
for i in range(len(n)-le,-1,-1):
    count=0
    for j in range(le):
        if m[j]==n[i+j]:
            count+=1
    if le-count==n[i:i+le].count('?'):
        n=n[0:i]+m+n[i+le:len(n)]
        check=1
        n=n.replace('?','a')
if check==1:
    print(n)
else:
    print("UNRESTORABLE")

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

def match(L):
    for l, t in zip(L, T):
        if l == '?':
            pass
        elif l != t:
            return False
        else:
            pass
    return True

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

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

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

def last_match(s, t):
    l, r = -1, -1
    for i in range(len(s)):
        if s[i] == "?" or s[i] == t[0]:
            match = True
            
            for j in range(len(t)):
                if i + j >= len(s) or (s[i+j] != '?' and s[i+j] != t[j]):
                    match = False
                    break
                
            if match:
                l, r = i, i + j
    return l, r
l, r = last_match(s, t)
if l == -1 and r == -1:
    print "UNRESTORABLE"
    exit()
res = ""
for i in range(len(s)):
    if i == l:
        match = True
        j = i
        res += t[0]
    elif i >= l and i <= r:
        res += t[i-j]
    elif s[i] == "?":
        res += "a"
    else:
        res += s[i]
print res

Solution (Python):
S = list(input())
T = input()
ans = "UNRESTORABLE"
for i in range(len(S)-len(T)+1):
    key = S[:]
    for n,t in enumerate(T):
        if key[i+n] not in ("?",t):break
        else:
            key[i+n] = t
        if n == len(T)-1:
            ans = "".join(key).replace("?","a")
print(ans)

Solution (Python):
S = input()
T = input()
L = []
slide_num = len(S) - len(T)
if slide_num < 0:
    print('UNRESTORABLE')
    exit()
for slide in range(slide_num+1):
    for j in range(len(T)):
        if S[j+slide] == T[j] or S[j+slide] == '?':
            None
        else:
            break
    else:
        L.append((S[:slide] + T + S[slide+len(T):]).replace('?', 'a'))

if L:
    print(L.pop())
else:
    print('UNRESTORABLE')

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappop, heappush, heapify, heappushpop
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S_ = list(input())
T = input()

for i in range(len(S_)-1, -1, -1):
	search = "".join(S_[i:i+len(T)])
	
	if len(search) == len(T):
		for j in range(len(search)):
			if not search[j] == "?" and search[j] != T[j]:
				break
		else:  
			S_[i:i+len(T)] = list(T)
			break
else:
	print("UNRESTORABLE")
	sys.exit(0)

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

print("".join(S_))

Solution (Python):

def main():
    s, t = [input().rstrip() for _ in range(2)]
    len_s, len_t = len(s), len(t)
    ans = []  

    for i in range(len_s - len_t + 1):
        p = s[i : i + len_t]  

        if all(j in (i, "?") for i, j in zip(t, p)):
            
            cand = (s[:i] + t + s[i + len_t :]).replace("?", "a")
            ans += [cand]

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


if __name__ == "__main__":
    main()

Solution (Python):
def output(S,T,i,j):
  op = ""
  for k in S[:j-i]:
    if k == "?":
      op += "a"
    else:
      op += k
  op += T
  if j-i+len(T) < len(S):
    for k in S[j-i+len(T):]:
      if k == "?":
        op += "a"
      else:
        op += k
  return op


S = input()
T = input()
SS = len(S)
TT = len(T)
cand = []
if TT > SS:
  print("UNRESTORABLE")
else:
  for i,t in enumerate(T):
    for j,s in enumerate(S):
      c = 0
      if ((t == s) or (s == "?")) and (i <= j) and (TT - i <= SS - j):
        for ss, tt in zip(S[j-i:], T):
          if ss != tt and ss != "?":
            break
          else:
            c += 1
        if c == TT:
          cand.append(output(S,T,i,j))
  cand.sort()
  cand.append("UNRESTORABLE")
  print(cand[0])

Solution (Python):
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

S = s()
T = s()

cands = set()
for i in range(len(S) - len(T) + 1):
    match = True
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            continue
        match = False
        break
    if match:
        cand = S[:i] + T + S[i+len(T):]
        scand = ''
        for c in cand:
            if c == '?':
                scand += 'a'
            else:
                scand += c
        cands.add(scand)

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

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

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

Solution (Python):

"""
Created on Sat Jun 22 08:35:15 2019

@author: Owner
"""

from collections import deque

Sd = input()
T = input()
S = deque()

Sr = list(reversed(Sd))
Tr = list(reversed(T))

flg_match = True    
for i,s in enumerate(Sr):
    flg_match = True    
    for j,t in enumerate(Tr):
        if i+j >= len(Sr):
            flg_match = False
            break
        if t == Sr[i+j] or Sr[i+j] == "?":
            pass
        else:
            flg_match = False
            break
        
    if flg_match:
        S.appendleft(T)
        
        break
    else:
        if s == "?":
            S.appendleft("a")
        else:
            S.appendleft(s)

if not flg_match:
    print("UNRESTORABLE")
else:    
    S = "".join(list(S))
    S = Sd[:len(Sd)-len(S)]+S
    S = S.replace("?", "a")
    
    print(S)

Solution (Python):
S=input()
T=input()
SS,TT,possible=len(S),len(T),[]

for i in range(SS-TT+1):
    key,memo=S,0
    for j in range(TT):
        if key[i+j]==T[j]:pass
        elif key[i+j]=="?":key=key[:i+j]+T[j]+key[i+j+1:]
        else:
            memo=1
            break
    if memo==0:
        key=key.replace("?","a")
        possible.append(key)

possible.sort()
possible=possible+["UNRESTORABLE"]
print(possible[0])

Solution (Python):
s = input()
t = input()
ans = set()
for i in range(len(s)-len(t)+1):
  cur = s[:i].replace("?", "a")
  for j in range(len(t)):
    if s[i+j]=="?" or s[i+j]==t[j]:
      cur += t[j]
    else:
      break
  cur += s[i+j+1:].replace("?", "a")
  if len(cur)==len(s):
    ans.add(cur)

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

Solution (Python):
s=input()
t=input()
l=len(t)
a=[]
for i in range(l-1,len(s)):
    x=s[len(s)-1-i:len(s)-1-i+l]
    cnt=0
    for j in range(l):
        if x[j]=="?" or x[j]==t[j]:
            cnt+=1
        else:
            break
    if cnt==l:
        u=s[:len(s)-1-i].replace("?","a")
        v=s[len(s)-1-i+l:].replace("?","a")
        key=u+t+v
        a.append(key)
if len(a)==0:
    print("UNRESTORABLE")
    exit()
a=sorted(a)
print(a[0])

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

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

pw = []
for i in range(len(S)-len(T)+1):
  if test(i):
    x = SA[:i] + T + SA[i+len(T):]
    pw.append(x)

if pw:
  answer = sorted(pw)[0]
else:
  answer = 'UNRESTORABLE'

print(answer)

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

def checkstr(ss):
  for (s, t) in zip(ss, T):
    check = True
    if s is '?':
      continue
    if s is not t:
      check = False
      break
  return check

tmp=''
ans='z'*50

for i in range(len(S)-len(T)+1):
  str = S[i:len(T)+i] 
  if checkstr(str):
    tmp = S[:i].replace("?","a")+T+S[i+len(T):].replace("?","a")
    ans = min(tmp, ans)
if tmp is '':
  ans = "UNRESTORABLE"
print(ans)

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

s_n = len(s)
t_n = len(t)


for i in range(s_n - t_n, -1,-1):
    
    match = s[i: i + t_n]
    
    isMatch=True
    for m_in,m_c in enumerate(match):
        if m_c == "?":
            continue
        
        if m_c != t[m_in]:
            isMatch = False
            break
    
    
    if not isMatch:
        continue

    
    password=""
    if i > 0:
        password = s[:i]
    password += t
    if s_n > i + t_n:
        password+=s[i+t_n:]

    password = password.replace('?', 'a')  
    print(password)
    exit()
    
print("UNRESTORABLE")

Solution (Python):

def solve(s, t):
    sl = len(s)
    tl = len(t)
    for i in range(sl-tl, -1, -1):
        for j, k in zip(range(i, i+tl), range(tl)):
            if s[j] == t[k] or s[j] == '?':
                if k == tl-1:
                    return (s[:i]+t+s[i+tl:]).replace('?', 'a')
            else:
                break

    return 'UNRESTORABLE'


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

Solution (Python):
from copy import deepcopy
s = input()
t = input()
l_t = len(t)

def check(x, y):
    for i, j in zip(x, y):
        if i == j or i == '?':
            continue
        else:
            return False
    return True


ans = 'z' * (len(s) + 1)
for k in range(len(s) - l_t + 1):
    x = s[k:k+l_t]
    if check(x, t):
        cand = list(deepcopy(s))
        cand[k:k+l_t] = list(t)
        cand = ''.join(cand)
        cand = cand.replace('?', 'a')
        if cand < ans:
            ans = cand

if not ans == 'z' * (len(s) + 1):
    print(ans)
else:
    print('UNRESTORABLE')

Solution (Python):

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

  if len(t) > len(s):
    print('UNRESTORABLE')
    return

  for i in range(len(s)-len(t), -1, -1):
    flag = True

    for j in range(len(t)):
      if s[i+j] != '?' and s[i+j] != t[j]:
        flag = False
        break

    if flag == True:
      str = s[0:i].replace('?', 'a') + t + s[i+len(t):].replace('?', 'a')
      print(str)
      return

  print('UNRESTORABLE')

main()

Solution (Python):
S = list(input())
T = list(input())
if len(S)<len(T):
    print("UNRESTORABLE")
else:
    S = S[::-1]
    T = T[::-1]
    k = 0
    l = 0
    for i in range(len(S)-len(T)+1):
        if S[i]=="?" or S[i]==T[0]:
            k = 0
            for j in range(len(T)):
                if S[i+j]=="?" or S[i+j]==T[j]:
                    k+=1
        if k==len(T):
            l = i
            break
    if k!=len(T):
        print("UNRESTORABLE")
    else:
        ans = []
        for i in range(len(S)):
            if l <= i and i < l+k:
                ans.append(T[i-l])
            elif S[i]=="?":
                ans.append("a")
            else:
                ans.append(S[i])
        print("".join(ans[::-1]))

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

Sdr = Sd[::-1].replace('?', '.')
Tr = T[::-1]

for i in range(len(Sdr)-len(Tr)+1):
    pattern = Sdr[i:i+len(Tr)]
    if re.match(pattern, Tr):
        print(Sdr.replace(pattern, Tr, 1).replace('.', 'a')[::-1])
        break
else:
    print('UNRESTORABLE')