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 = len(S)
t = len(T)
f = True
ans = []
for i in range(s):
    tmp = list(S)
    if t+i>s:
        continue
    else:
        for j in range(t):
            tmp2 = list(T)
            if tmp[i+j]=='?':
                tmp[i+j] = tmp2[j]
            elif tmp[i+j]== tmp2[j]:
                continue
            else:
                f = False
        if f:
            ans.append(tmp)
    f = True
res = []
for a in ans:
    for i in range(len(a)):
        if a[i]=='?': a[i]='a'
    res.append(''.join(a))
print(min(res) if res else 'UNRESTORABLE')

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

if len(S) < len(A):
    print('UNRESTORABLE')
    exit(0)


for i in reversed(range(len(S)-len(A)+1)):
    for j in range(len(A)):
        if not(S[i+j] == A[j] or S[i+j] == '?'):
            break
    else:
        ans = S[:i] + A + S[i+len(A):]
        ans = ans.replace('?', 'a')
        print(ans)
        break
else:
    print('UNRESTORABLE')

Solution (Python):
import sys


def compare(s: str, t: str) -> bool:
    return all(x == y or x == "?" for x, y in zip(s, t))


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

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

    indexes = []
    for i in range(len_s):
        if i + len_t <= len_s:
            s = S[i : i + len_t]
            if compare(s, T):
                indexes.append(i)

    if len(indexes) == 0:
        print("UNRESTORABLE")
        sys.exit(0)

    decrypted = []
    for j in indexes:

        d = ""
        for i, s in enumerate(S):
            if i in range(j, j + len_t):

                d += T[i - j]
                continue
            if s == "?":
                d += "a"
                continue

            d += s

        decrypted.append(d)
    decrypted.sort()
    print(decrypted[0])

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


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

    SN = len(S)
    TN = len(T)

    ans = []
    for i in range(SN - TN + 1):
        for j in range(TN):
            if S[i + j] == '?' or S[i + j] == T[j]:
                continue
            else:
                break
        else:
            ts = list(S)
            for j in range(TN):
                ts[i + j] = T[j]
            ans.append(ts)

    if len(ans) == 0:
        print('UNRESTORABLE')
    else:
        for i in range(len(ans)):
            ans[i] = ''.join(ans[i]).replace('?', 'a')
        print(sorted(ans)[0])


if __name__ == '__main__':
    main()

Solution (Python):
import re

sdash = input().replace('?', '.')
t = input()
len_t = len(t)
isNotUsed = True

for i in range(len(sdash) - len_t, -1, -1):
    if re.match(sdash[i:i+len_t], t):
        s = sdash[:i] + t + sdash[i+len_t:]
        isNotUsed = False
        break
if isNotUsed:
    print('UNRESTORABLE')
else:
    print(s.replace('.', 'a'))

Solution (Python):
import sys
S = input()
T = input()
match_list = []
for i in range(len(S)-len(T)+1):
    is_match = True
    for j in range(len(T)):
        if S[i+j] != T[j] and S[i+j] != '?':
            is_match = False
            break
    if is_match:
        match_list.append(i)
if not match_list:
    print("UNRESTORABLE")
    sys.exit()
ans = S[:max(match_list)] + T
if len(S) > len(ans):
    ans += S[len(ans):]
print(ans.replace('?', 'a'))

Solution (Python):
S = input()
T = input()
l = len(S)
lt = len(T)
ok = False
if l >= lt:
    for i in range(l - lt, -1, -1):
        SS = S[i:i+lt]
        can_replace = True
        for j, ss in enumerate(SS):
            can_replace *= (SS[j] == T[j] or SS[j] == '?')
        if can_replace:
            ok = True
            t_i = i
            break
if ok:
    S = S.replace('?', 'a')
    print(S[:t_i] + T + S[t_i + lt:])
else:
    print('UNRESTORABLE')

Solution (Python):
S_p = input()
T = input()
S_len = len(S_p)
T_len = len(T)

if S_len < T_len:
    print("UNRESTORABLE")
    exit(0)

ans = []
for offset in range(S_len - T_len + 1):
    isRestorable = True
    for d in range(T_len):
        if S_p[offset+d] == "?":
            continue
        elif S_p[offset+d] != T[d]:
            isRestorable = False
            break
        else:
            pass
    if isRestorable:
        tmp = S_p[:offset] + T + S_p[offset+T_len:]
        ans.append(tmp.replace("?", "a"))
if len(ans) == 0:
    print("UNRESTORABLE")
else:
    ans.sort()
    print(ans[0])

Solution (Python):
s = input()
t = input()
ans = ""
for i in range(len(s)-len(t)+1):
    flag = 0
    for j in range(len(t)):
        if s[i+j]==t[j] or s[i+j]=="?":
            flag += 1
    if flag==len(t):
        tnp = ""
        for j in range(len(s)):
            if (s[j]!="?"):
                tnp += s[j]
            else:
                if (i<=j and j<i+len(t)):
                    tnp += t[j-i]
                else:
                    tnp += "a"
        ans = tnp
if (len(ans)>0):
    print(ans)
else:
    print("UNRESTORABLE")

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)



class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]




s = input()
t = input()
n = len(s)
m = len(t)
unrestorable = True
a = 0
for i in reversed(range(n)):
    if i-m+1 < 0:
        break
    t2 = s[i-m+1:i+1]
    ok = True
    for j in range(m):
        if t2[j] != "?" and t2[j] != t[j]:
            ok = False
            break
    if ok:
        
        a = i-m+1
        unrestorable = False
        break
if unrestorable:
    print("UNRESTORABLE")
else:
    
    s2 = s[:a] + t + s[a+m:]
    
    ans = ""
    for i in s2:
        if i == "?":
            ans += "a"
        else:
            ans += i
    print(ans)

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

if not "?" in s:
    if ("".join(t) in "".join(s)):
        print("".join(s))
    else:
        print("UNRESTORABLE")
    exit()

ans=[]
switch=1
p=len(s)-len(t)
for i in range(p+1):
    for j in range(len(t)):
        if s[j+i]!=t[j] and s[j+i]!="?":
            switch=0
    if switch==1:
        c_s=s[:]
        for j in range(len(t)):
            c_s[j+i]=t[j]
        for j in range(len(c_s)):
            if c_s[j]=="?":
                c_s[j]="a"
        ans.append("".join(c_s))
    switch=1

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

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

    sl = len(S)
    tl = len(T)

    
    check = False
    for i in range(sl - tl, -1, -1):
        for j in range(tl):
            if S[i + j] == T[j] or S[i + j] == '?':
                check = True
            else:
                check = False
                break
        if check:
            print((S[:i] + T + S[i + tl:]).replace('?', 'a'))
            break
    if check is False:
        print('UNRESTORABLE')

        
resolve()

Solution (Python):
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
t = input()

if len(s) < len(t):
    ans = "UNRESTORABLE"
else:
    reg = -1
    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:
            reg = i
            break
    if reg == -1:
        ans = "UNRESTORABLE"
    elif reg == len(s)-len(t):
        s = s[:i]+t
        ans = s.replace('?','a')
    else:
        s = s[:i]+t+s[i+len(t):]
        ans = s.replace('?','a')
print(ans)

Solution (Python):
def restore(s, t):
    orig_s = s
    for i in range(len(s) - len(t), -1, -1):
        s = list(orig_s)
        ok = True
        for j in range(len(t)):
            if s[i+j] != t[j] and s[i+j] != '?':
                ok = False
                break
            s[i+j] = t[j]
        if ok:
            for i in range(len(s)):
                if s[i] == '?':
                    s[i] = 'a'
            return "".join(s)
    return "UNRESTORABLE"


S = input()
T = input()

print(restore(S, T))

Solution (Python):
s = input()[::-1]
t = input()[::-1]
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()
    
tmp = s.replace("?", "a")
if t in tmp:
    print(tmp[::-1])
    exit()
    
for i in range(len(s) - len(t) + 1):
    for j in range(len(t)):
        if s[i + j] == "?":
            continue
        elif s[i + j] == t[j]:
            continue
        break
    else:
        ans = ""
        for k in range(i):
            if s[k] == "?":
                ans += "a"
            else:
                ans += s[k]
        for tt in t:
            ans += tt
        for k in range(len(t) + i, len(s)):
            if s[k] == "?":
                ans += "a"
            else:
                ans += s[k]
        print(ans[::-1])
        exit()
print("UNRESTORABLE")

Solution (Python):
s = input()[::-1]
t = input()[::-1]
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()
    
for i in range(len(s) - len(t) + 1):
    for j in range(len(t)):
        if s[i + j] == "?":
            continue
        elif s[i + j] == t[j]:
            continue
        break
    else:
        ans = ""
        for k in range(i):
            if s[k] == "?":
                ans += "a"
            else:
                ans += s[k]
        for tt in t:
            ans += tt
        for k in range(len(t) + i, len(s)):
            if s[k] == "?":
                ans += "a"
            else:
                ans += s[k]
        print(ans[::-1])
        exit()
print("UNRESTORABLE")

Solution (Python):
S = input()
T = input()
ans = []

lt = len(T)
for i in range(len(S)-lt+1):
    s = S[i:i+lt]
    for t in range(lt):
        if s[t]=='?':
            if t != lt-1:
                pass
            else:
                ans.append((S[:i]+T+S[i+lt:]).replace('?', 'a'))
        else:
            if s[t]!=T[t]:
                break
            elif t==lt-1:
                ans.append((S[:i]+T+S[i+lt:]).replace('?', 'a'))
                break

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

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

n = len(sd)
m = len(t)
s = []

for i in range(n - m, -1, -1):
    t_kamo = sd[i:i + m]
    for j in range(m + 1):
        if j == m:
            print((sd[:i] + t + sd[i + len(t):]).replace("?", "a"))
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:
            break

print("UNRESTORABLE")

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

Solution (Python):
import re

def main():
    s = input().replace("?", ".")
    t = input()
    ans = []

    for i in range(len(s)-len(t)+1):
        if not re.match(s[i:i+len(t)], t):
            continue
        tmp = (s[:i] + t + s[i+len(t):]).replace(".", "a")
        ans.append(tmp)

    if not ans:
        print("UNRESTORABLE")
    else:
        print(min(ans))

if __name__ == "__main__":
    main()

Solution (Python):
import re
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


sd = S()
sd = sd.replace('?', '.')  
t = S()



ans = []
for i in range(len(sd) - len(t) + 1):
    if not re.match(sd[i:i + len(t)], t):
        continue
    kouho = (sd[:i] + t + sd[i + len(t):]).replace('.', 'a')
    ans.append(kouho)

if not ans:
    print('UNRESTORABLE')
    sys.exit()

print(min(ans))

Solution (Python):
s = input()[::-1]
t = input()[::-1]
l = []
pre = 0

for i in range(len(s)):
    l.clear()
    cnt = 0
    pre = i
    while cnt < len(t) :
        if pre + cnt >= len(s):
            break
        if s[i] == t[cnt] or s[i] == "?":
            l.append(t[cnt])
            i += 1
            cnt += 1
        else:
            break
    else:
        ans = s[:pre] + "".join(l) + s[i:]
        ans = ans.replace("?", "a")
        ans = ans[::-1]
        print(ans)
        exit()
print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
ans = "UNRESTORABLE"
for begin in range(len(s)):
    if begin+len(t) > len(s):
        break
    tmp = list(s)
    ti = 0
    for i in range(begin,begin+len(t)):
        tmp[i] = t[ti]
        ti += 1
    for i in range(len(s)):
        if s[i] != '?':
            if s[i] != tmp[i]:
                break
    else:
        for i in range(len(s)):
            if tmp[i] == "?":
                tmp[i] = 'a'
        ans = "".join(tmp)
print(ans)

Solution (Python):
s=input()
t=input()
l=[]
import sys
if len(t)>len(s):
  import sys
  print("UNRESTORABLE")
  exit()
  
for i in range(len(s)-len(t)+1):
  z=s[:i]
  for j in range(i,i+len(t)):
    if s[j]!=t[j-i] and s[j]!="?":
      break
    else:
      z+=t[j-i]
  else:
    z+=s[i+len(t):]
    l.append(z.replace("?","a"))
l.sort()
print(l[0] if len(l) else "UNRESTORABLE")

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

for i in range(len(s) - len(t) + 1)[::-1]:
    for j, (ns, nt) in enumerate(zip(s[i:i+len(t)], t)):
        if ns != nt and ns != '?':
            break
        if j == len(t) -1:
            ans = s[:i] + t + s[i+len(t):]
            print(ans.replace('?', 'a'))
            exit()
print('UNRESTORABLE')

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

for i in range(len(s) - len(t) + 1)[::-1]:
    cnt = 0
    for ns, nt in zip(s[i:i+len(t)], t):
        cnt += 1
        if ns != nt and ns != '?':
            break
        if cnt == len(t):
            ans = s[:i] + t + s[i+len(t):]
            print(ans.replace('?', 'a'))
            exit()
print('UNRESTORABLE')

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

ans = 'UNRESTORABLE'
def test(i):
    for ns, nt in zip(s[i:i+len(t)], t):
        if ns == '?':
            continue
        if ns != nt:
            return False
    return True

for i in range(len(s) - len(t) + 1):
    if test(i):
        re_s = s.replace('?', 'a')
        ans = re_s[:i] + t + re_s[i+len(t):]

print(ans)

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

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

print("UNRESTORABLE")

Solution (Python):
S=input()
T=input()
for i in range(len(S)-1,len(T)-2,-1):
  a=''
  for j in range(len(T)):
    if S[i+1-len(T)+j]=='?':
      a=a+T[j]
    else:
      a=a+S[i+1-len(T)+j]
  if a==T:
    U=S[:i+1-len(T)]+a+S[i+1-len(T)+len(T):]
    c=''
    for k in range(len(U)):
      if U[k]=='?':
        c=c+'a'
      else:
        c=c+U[k]
    print(c)
    exit()
print('UNRESTORABLE')

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

if len(t) > len(s):
    print("UNRESTORABLE")
    exit()

s = s[::-1]
t = t[::-1]
for i in range(len(s)-len(t)+1):
    k = 0
    while True:
        if k == len(t):
            ans = s[:i] + t + s[i+len(t):]
            print(ans[::-1].replace("?","a",len(ans)))
            exit()
        if t[k] == s[i+k] or s[i+k] == "?":
            k += 1
        else:
            break
print("UNRESTORABLE")

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

s= s.replace('?','.')

n_s = len(s)
n_t = len(t)

res = []

for i in range(n_s-n_t+1):
    m = re.match(s[i:i+n_t],t)
    if not m:continue
    res.append((s[:i]+t+s[i+n_t:]).replace('.','a'))
if not res:
    print('UNRESTORABLE')
else:
    print(min(res))

Solution (Python):
def readinput():
    s=input()
    t=input()
    return s,t

def main(s,t):
    found=False
    it=0
    for i in range(len(s)-len(t),-1,-1):
        for j in range(len(t)):
            
            if s[i+j]=='?':
                continue
            if s[i+j]!=t[j]:
                break
        else:
            found=True
            it=i
            break
    if found==False:
        return 'UNRESTORABLE'

    ans=s[:it]+t+s[it+len(t):]
    ans=ans.replace('?','a')

    return ans

if __name__=='__main__':
    s,t=readinput()
    ans=main(s,t)
    print(ans)

Solution (Python):
s=input()
t=input()
l=[]
check=0
for i in range(len(s)-len(t)+1):
    c=0
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?":
            break
        else:
            c+=1
    if c==len(t):
        check+=1
        l.append(s[:i]+t+s[i+j+1:])
    

if check==0:
    print("UNRESTORABLE")
else:
    L=[]
    for i in range(len(l)):
        x=""
        for j in range(len(s)):
            if l[i][j]=="?":
                x+="a"
            else:
                x+=l[i][j]
        L.append(x)
    L.sort()
    print(L[0])

Solution (Python):
S = list(input())
T = list(input())
import copy
ans = []
for i in reversed(range((len(S)-len(T))+1)):
    R = copy.deepcopy(S)
    for j in range(len(T)):
        if S[i+j] != "?" and S[i+j] != T[j]:
            break
    else:
        for j in range(len(T)):
            R[i+j] = T[j]
        for j in range(len(S)):
            R[j] = "a" if R[j] == "?" else R[j]
        ans.append("".join(R))
if ans:
    ans.sort()
    print(ans[0])
else:
    print("UNRESTORABLE")

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

S = list(s())
T = list(s())
l1 = len(S)
l2 = len(T)
Flag =  0
for i in range(l1-l2+2):
    if S[i:i+l2] == T:
        Flag = 1
        break
if Flag == 1:
    for i in range(l1):
        if S[i] == '?':
            S[i] = 'a'
    print(''.join(S))
    exit()
for i in range(l1-l2,-1,-1):
    s = S[i:i+l2]
    Flag = 1
    for j in range(l2):
        if s[j] != T[j] and s[j] != '?':
            Flag = 0
            break
    if Flag:
        for j in range(i,i+l2):
            if S[j] == '?':
                S[j] = T[j-i]
        for i in range(l1):
            if S[i] == '?':
                S[i] = 'a'
        print(''.join(S))
        exit()
print('UNRESTORABLE')

Solution (Python):
import copy
S=input()
N=len(S)
SS=[]
for i in range(N):
    SS.append(S[i])

T=input()
M=len(T)
TT=[]

for i in range(M):
    TT.append(T[i])

if N<M:
    print("UNRESTORABLE")
else:
    for i in range(N-M+1):
        flag=True
        tmp=copy.deepcopy(SS)
        for j in range(M):
            
            if SS[N-1-i-j]==TT[M-1-j] or SS[N-1-i-j]=="?":
                tmp[N-1-i-j]=TT[M-1-j]
            else:
                flag=False
                break
        
        if flag==True:
            Z=[str(a) for a in tmp]
            Z="".join(Z)
            

            ans=Z.replace("?","a")
            print(ans)
            exit()
    print("UNRESTORABLE")

Solution (Python):
import re
Sp = input().replace('?','.')
T = input()
flag = 0
for i in range(len(Sp)-len(T),-1,-1):
    if re.match(Sp[i:i+len(T)],T):
        Sp = Sp[:i]+T+Sp[i+len(T):]
        flag = 1
        break
if flag == 1:
    print(Sp.replace('.','a'))
else:
    print('UNRESTORABLE')

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

if len(S_) < len(T):
  print('UNRESTORABLE')
  exit()

i_ = -1

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:
      i_ = i

if i_ == -1:      
  print('UNRESTORABLE')
else: 
  if i_ + len(T) == len(S_):
    print(S_[:i_].replace('?','a')+T)
  else:
    print(S_[:i_].replace('?','a')+T+S_[i_+len(T):].replace('?','a'))

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

if len(s)<l:
    print('UNRESTORABLE')   
    exit() 
for i in range(len(s),-1+l,-1):
    tmp = s[i-l:i]
    tmp_flag = False
    for j in range(l):
        if tmp[j]=='?':
            tmp_flag = True
        elif tmp[j]!=t[j]:
            tmp_flag = False
            break
        else:
            tmp_flag = True
    if tmp_flag:
        t = list(t)
        s = list(s)
        s[i-l:i] = t
        break

if tmp_flag:
    for i in range(len(s)):
        if s[i]=='?':
            s[i] = 'a'
    print(''.join(s))
else:
    print('UNRESTORABLE')

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

if len(S_dash) < len(T):
    print("UNRESTORABLE")
    exit()

else:
    res = "UNRESTORABLE"
    rev_S = S_dash[::-1]
    rev_T = T[::-1]
    for i in range(len(rev_S)-len(rev_T)+1):
        check = rev_S[i:i+len(rev_T)]
        t_ok = [False] * len(rev_T)
        for j, (c, t) in enumerate(zip(check, rev_T)):
            if c == t or c == "?":
                t_ok[j] = True
        if sum(t_ok) == len(t_ok):
            res = list(rev_S)
            for j,t in enumerate(rev_T):
                res[i+j] = t
            res = "".join(res[::-1])
            res = res.replace("?", "a")
            print(res)
            exit()

    print(res)

Solution (Python):









s_dash = list(str(input()))
t = list(str(input()))

lt = len(t)
ls = len(s_dash)
msg = "UNRESTORABLE"


if ls < lt:
    print(msg)
    exit()


candidate = []
for i in range(ls-lt+1):
    is_matched = True
    tmp = s_dash.copy()

    for j in range(lt):
        if t[j] == s_dash[i+j] or s_dash[i+j] == '?':
            tmp[i+j] = t[j]
        else:
            is_matched = False
            break

    if is_matched == True:
        for j in range(ls):
            if tmp[j] == '?':
                tmp[j] = 'a'
        
        cand = str("")
        for c in tmp:
            cand += c
        candidate.append(cand)

if len(candidate) == 0:
    print(msg)
else:
    candidate.sort()
    print(candidate[0])

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

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

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

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

    ans_list = []

    flag = False
    for i in range(len_s-len_t+1):
        tmp_s = s[i:i+len_t]
        for j in range(len_t):
            if tmp_s[j] != t[j] and tmp_s[j] != '?':
                break
        else:
            flag = True
            ans_s = s[0:i] + t + s[i+len_t:len_s]
            ans_list.append(ans_s.replace('?', 'a'))

    if not flag:
        print('UNRESTORABLE')
        exit()

    ans_list.sort()

    print(ans_list[0])
 
if __name__ == '__main__':
    main()

Solution (Python):
S = input().strip()
N=len(S)
T = input().strip()
M=len(T)
A = []
for i in range(N-M+1):
    flag = 0
    for j in range(i,i+M):
        if S[j]=="?" or S[j]==T[j-i]:continue
        else:
            flag=1
            break
    if flag==0:
        X = list(S)
        for j in range(i,i+M):
            X[j] = T[j-i]
        for i in range(N):
            if X[i]=="?":
                X[i]="a"
        A.append(X)
if len(A)==0:
    print("UNRESTORABLE")
else:
    A = sorted(A)
    print("".join(A[0]))

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

n = len(s_)
m = len(t)

if m > n:
    print('UNRESTORABLE')
    exit()

import copy
ans = []
for i in range(n-m+1):
    if s_[i] == t[0] or s_[i] == '?':
        temp = copy.copy(s_)
        for j in range(i, i+m):
            
            if s_[j] == '?':
                temp[j] = t[j-i]
            elif s_[j] == t[j-i]:
                continue
            else:
                break
        else:
            for k in range(n):
                if temp[k] == '?':
                    temp[k] = 'a'
            ans.append(''.join(temp))
if ans:
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
S = input().replace('?','.')
T = input()
for i in range(len(S)-len(T),-1,-1):
    if re.match(S[i:i+len(T)],T):
        S = S.replace('.','a')
        print(S[:i] + T + S[i+len(T):])
        exit()
print('UNRESTORABLE')