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.



Sample input:
?tc????
coder


Sample output:
atcoder

Submission status: Accepted

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

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans = 'UNRESTORABLE'
memo = -1

for i in range(len(S) - len(T) + 1):
    cnt = 0
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            cnt += 1
        else:
            break
        if cnt == len(T):
            ans = T
            memo = i


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

Submission status: Accepted

Solution code:
from itertools import zip_longest

S = input()
T = input()

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

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

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

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

Submission status: Accepted

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

ans = "UNRESTORABLE"

memo = -1

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S_ = input()
T = input()

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

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

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

Submission status: Accepted

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

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

    print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def c_dubious_document_2(S, T):
    # S:鍵の文字列のいくつかを'?'で置き換えたもの
    # T:Sの中に含まれている可能性のある文字列
    tmp = ''  # 文字列の候補があれば、それが格納される
    for i in range(len(S) - len(T) + 1):
        # Sの中にTを置く場所がないままTがSの末尾をはみ出すなら、復元不可能が確定
        for j in range(len(T)):
            if S[i + j] == '?' or S[i + j] == T[j]:
                # Sのi文字目以降にTを埋め込める可能性がある
                continue
            break  # if文が成り立たないなら、埋め込めない
        else:
            # TをSのi文字目以降に埋め込める。候補が複数あれば、最も右に埋め込む。
            tmp = S[:i] + T + S[i + len(T):]
    # 候補があれば、辞書順最小にするために'?'を'a'で置換する
    ans = tmp.replace('?', 'a') if tmp else 'UNRESTORABLE'
    return ans

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

S = raw_input()
T = raw_input()

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

Submission status: Accepted

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

import sys

##後ろから順に考える
code=list(S)
code.reverse()
S=list(S)
S.reverse()
T=list(T)
T.reverse()


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

Submission status: Accepted

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


s = input()
t = input()

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

Submission status: Accepted

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

print(f())

Submission status: Accepted

Solution code:
s, T = input()[::-1], input()[::-1]
#後ろから見る

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

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

Submission status: Accepted

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

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


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

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

print("UNRESTORABLE")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
NOT_FOUND = 'UNRESTORABLE'


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

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


print(m())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

s = input()
t = input()

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

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

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

Submission status: Accepted

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

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

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

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

    print("".join(s_d))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections

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


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



if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

print("UNRESTORABLE")

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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


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

Submission status: Accepted

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

Submission status: Accepted

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

candidate=[]
count=0

for i in range(len(S)-len(T)+1):#Sの上を走る時のスタート地点
    for j in range(len(T)):#Tの上を走る
        if match(T[j],S[i+j]):
            count+=1
        else:
            count=0
            break#一つでも違ったら次のiへ
    if count==len(T):
        count=0
        U=S[:i]+T+S[i+len(T):]
        U=U.replace('?','a')
        candidate.append(U)
        
if len(candidate)==0:
    print('UNRESTORABLE')
else:
    candidate.sort()
    print(candidate[0])

Submission status: Accepted

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

candidate=[]
count=0

for i in range(len(S)-len(T)+1):#Sの上を走る時のスタート地点
    for j in range(len(T)):#Tの上を走る
        if match(T[j],S[i+j]):
            count+=1
        else:
            count=0
            break#一つでも違ったら次のiへ
    if count==len(T):
        count=0
        U=S[:i]+T+S[i+len(T):]
        U=U.replace('?','a')
        candidate.append(U)
        
if len(candidate)==0:
    print('UNRESTORABLE')
else:
    
    print(sorted(candidate)[0])

Submission status: Accepted

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

Submission status: Accepted

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

if not ans:
  print('UNRESTORABLE')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#abc076c
import fnmatch,sys

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

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

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(input().split())
    return li

# --------------------------------------------

dp = None

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

    is_restorable = False
    replace_start_idx = None
    replace_end_idx = None

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

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

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def check(S, T):
    ans="~"
    for i in range(len(S)-len(T)+1):
        found=True
        for j in range(len(T)):
            if S[i+j] != "?" and T[j] != S[i+j]:
                found=False
                break
        if found: 
            ans=min(ans, S[:i].replace("?", "a")+T+S[i+len(T):].replace("?","a"))

    return ans


S=input()
T=input()

ans=check(S, T)
if ans == "~":
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

Solution code:
def main():
    S_ = input()
    T = input()

    f(S_, T)


def f(s, t):
    l_t = len(t)
    for i in reversed(range(len(s)-l_t+1)):
        b = s[i:i+l_t]
        c = sum(c1 == c2 or c2 == "?" for c1, c2 in zip(t, b))

        if c == len(t):
            ans = s[:i].replace("?", "a") + t + s[i+l_t:].replace("?", "a")
            print(ans)
            return

    print("UNRESTORABLE")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from fnmatch import fnmatch

for i in range(len(S) - len(T), -1, -1):
    if fnmatch(T, S[i:i + len(T)]):
        r = S[:i] + T + S[i + len(T):]
        r = r.replace("?", "a")
        print(r)
        break
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = input()
t = input()
ns = len(s)
nt = len(t)
ss = []

def match(s,t):
    for i,j in zip(s,t):
        if i == '?':
            continue
        if i != j:
            return False
    return True
    
for i in range(ns-nt+1):
    if match(s[i:i+nt], t):
        s2 = s[:i] + t + s[i+nt:]
        s3 = s2.replace('?','a')
        ss.append(s3)
        
if len(ss) == 0:
    print('UNRESTORABLE')
else:
    print(list(sorted(ss))[0])

Submission status: Accepted

Solution code:
Sd = input()
T = input()
if len(Sd) < len(T):
    print("UNRESTORABLE")
else:
    D = []
    for i in range(len(Sd) - len(T) + 1):
        flag = 1
        S = Sd
        for j in range(len(T)):
            if Sd[i+j] == "?" or Sd[i+j] == T[j]:
                S = S[:i+j]+T[j]+S[i+j+1:]
            else:
                flag = 0
        if flag == 1:
            D.append(S.replace("?", "a"))
    D.sort()
    if len(D) == 0:
        print("UNRESTORABLE")
    else:
        print(D[0])

Submission status: Accepted

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

n = len(S)
m = len(T)

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

p = []
for i in range(n-m+1):
    s = S[i:i+m]
    if check(s, T):
        p.append(i)

if p == []:
    print("UNRESTORABLE")
    exit()

plast = p[-1]
ans = S[:plast] + T + S[plast+m:]
print(ans.replace("?", "a"))