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=input()
t=input()
n=len(s)
m=len(t)
li=[]
for i in range(n-m+1):
  u=s[i:i+m]
  c=0
  for j in range(m):
    if u[j]!="?" and u[j]!=t[j]:
      c=1
      break
  if c==0:
    if i==0:
      v=t+s[i+m:]
    elif i==n-m:
      v=s[:i]+t
    else:
      v=s[:i]+t+s[i+m:]
    for k in range(n):
      if v[k]=="?":
        v=v[:k]+"a"+v[k+1:]
    li.append(v)
if li==[]:
  print("UNRESTORABLE")
else:
  li.sort()
  print(li[0])

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""
# 最大公約数 と 最小公倍数
"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7

def main():
  import re
  #?tc????
  S = input().replace('?','.')
  T = input()
  #print(S)
  #.tc....
  #print(T)
  #coder
  # print(len(S) - len(T)) 7:5
  # 2
  for i in range(len(S) - len(T), -1,-1):
    # print(re.match(S[i:i+len(T)],T))
    # <_sre.SRE_Match object; span=(0, 5), match='coder'>
    # 後ろから、len(T)文まで見て、そこにTが入るかを検証
    # 入るなら Trueがかえる？
    if re.match(S[i:i+len(T)],T):
      #print(S) 
      S = S.replace('.','a')
      # すべてをaに置き換える(辞書順最初)
      print(S[:i] + T + S[i+len(T):])
      # i-1番目までSの部分、iからi+len(T)-1まではTを入れ（入れれることが確認できたから)
      # そして、i+len(T)番目から最後までSの部分を入れる
      exit()
  print('UNRESTORABLE')
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys

S = input()
T = input()
lenS = len(S)
lenT = len(T)
ans = ""
for i in range(lenS-lenT,-1,-1):
    for j in range(lenT):
        if S[i+j]!=T[j] and S[i+j]!="?":
            break
    else:
        ans = S[:i]+T+S[i+lenT:]
        ans = ans.replace("?","a")
        print(ans)
        sys.exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    N = len(S)
    M = len(T)
    ans = "z" * N
    for i in range(N - M + 1):
        _S = S[i:i + M]
        is_ok = True
        for j in range(M):
            if _S[j] == "?":
                pass
            elif _S[j] == T[j]:
                pass
            else:
                is_ok = False
        if is_ok:
            candidate = S[:i] + T + S[i + M:]
            candidate = candidate.replace("?", "a")
            if candidate < ans:
                ans = candidate

    if ans == "z" * N:
        print("UNRESTORABLE")
    else:
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S=input()
ls=len(S)
T=input()
lt=len(T)
ans="z"*51
for i in range(ls-lt+1):
  s=S[i:i+lt]
  hamaru=True
  now="z"*51
  for j in range(lt):
    if s[j]!="?" and s[j]!=T[j]:
      hamaru=False
  #print(hamaru)
  if hamaru:
    now=""
    for k in range(ls):
      if k in list(range(i,i+lt)):
        now+=T[k-i]
      elif S[k]=="?":
        now+="a"
      else:
        now+=S[k]
  ans=min(ans,now)
if ans=="z"*51:
  ans="UNRESTORABLE"
print(ans)

Submission status: Accepted

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


S = str(input())
T = str(input())

ans = ""
for i in reversed(range(len(S)-len(T)+1)):
    # S[i:i+len(T)]で対象の範囲を取れる
    if check(S[i:i+len(T)], T):
        num = 0
        while num != len(S):
            if num == i:
                ans += T
                num += len(T)
            else:
                if S[num] == "?":
                    ans += "a"
                else:
                    ans += S[num]
                num += 1
        break

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

Submission status: Accepted

Solution code:
s = input()
t = input()
lenS = len(s)
lenT = len(t)

if lenS < lenT:
    print("UNRESTORABLE")
    exit()

ans = []
for i in range(lenS - lenT + 1):
    tmp = list(s)
    flag = True
    for j in range(lenS):
        if j < i or i + lenT <= j:
            if tmp[j] == '?':
                tmp[j] = 'a'
        else:
            if tmp[j] == '?':
                tmp[j] = t[j - i]
            elif tmp[j] != t[j - i]:
                flag = False
                break

    if flag:
        ans.append("".join(tmp))

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

Submission status: Accepted

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

ans = []
if len(s)>=len(t):    
    for i in range(len(s)-len(t)+1):
        cnt=0
        tmp=list(s)
        for j in range(len(t)):
            if s[i+j]!=t[j] and s[i+j]!='?':
                break
            else:
                cnt+=1
                tmp[i+j]=t[j]
        if cnt==len(t):
            tmp="".join(tmp)
            ans.append(tmp.replace('?','a'))
ans = sorted(ans)
if not ans:
    print('UNRESTORABLE')
else:
    print(ans[0])

Submission status: Accepted

Solution code:
s = input()
t = input()
rs = list(reversed(s))
rt = list(reversed(t))
for i in range(len(s)-len(t)+1):
         for j in range(len(t)):
                  if not (rs[i+j] == "?" or rs[i+j] == rt[j]):
                           break
         else:
                  for k in range(len(t)):
                           rs[i+k] = rt[k]
                  for l in range(len(s)):
                           if rs[l] == "?":
                                    rs[l] = "a"
                  ans = list(reversed(rs))
                  answer = "".join(ans)
                  print(answer)
                  exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


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

    # 初期値の設定(結果が偽の場合の値を初期値にしてしまう)
    ans = "UNRESTORABLE"

    # i文字目から代入した場合、を検証していく
    for i in range(len(s)-len(t)+1):

        # iから一文字ずつ、不正な点が無いか検証。

        for j in range(len(t)):
            isRestorable = True

            if not s[j+i] == t[j] and not s[j+i] == "?":
                isRestorable = False
                break

        # 文字が代入できるなら、代入した場合の文字を変数に入れる。
        # これをループしていくので、ansには最終的に、辞書最小値が格納される、というロジック。
        if isRestorable:
            ans = s[0:i] + t + s[i+len(t):len(s)]
            ans = ans.replace("?", "a")

    print(ans)

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)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
S = input()
T = input()
S2 = S.replace('?','a')
ans = []

for i in range(len(S)-len(T)+1):
    S_sub = S[i:i+len(T)]
    
    check = True
    
    for s,t in zip(S_sub,T):
        if s != t and s == '?':
            continue
        elif s == t:
            continue
        elif s != t and s != '?':
            check = False
            break
        
    if check == True:
        tmp = S2[:i] + T + S2[i+len(T):]
        ans.append(tmp)
        
if len(ans) != 0:
    answer = sorted(ans)[0]
else:
    answer = 'UNRESTORABLE'
    
print(answer)

Submission status: Accepted

Solution code:
s=input()
t=input()
b=[]
if len(s)<len(t):
    print('UNRESTORABLE')
    exit()
for i in range(len(s)-len(t)+1):
    a=1
    c=''
    for j in range(len(t)):
        if s[i+j]=='?' or s[i+j]==t[j]:
            continue
        else:
            a=0
    if a==1:
        for j in range(i):
            c+=s[j] if s[j]!='?' else 'a'
        for j in range(len(t)):
            c+=t[j]
        for j in range(len(s)-len(t)-i):
            c+=s[j+i+len(t)] if s[j+i+len(t)]!='?' else 'a'
        b.append(c)
if len(b)==0:
    print('UNRESTORABLE')
else:
    print(min(b))

Submission status: Accepted

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

S.reverse()
T.reverse()
flag = False

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

        if j == len(T)-1:
            flag = True
            for k in range(len(T)):
                if S[k+i] == '?':
                    S[k+i] = T[k]
            break
    if flag:
        break

if flag:
    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'
    S.reverse()
    print(''.join(S))

else:
    print('UNRESTORABLE')

Submission status: Accepted

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

result = []
for i in range(len(s)-len(t)+1):
    temp = '?'*i + t + '?'*(len(s)-len(t)-i)
    
    ans = ''
    flag = True
    for j in range(len(s)):
        if s[j] == '?' or s[j] == temp[j]:
            ans += temp[j]
        elif s[j] != '?' and temp[j] == '?':
            ans += s[j]
        else:
            flag = False

    if flag:
        result.append(ans.replace('?', 'a'))

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

Submission status: Accepted

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

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

    for j in range(len(T)):
        if tmp[j]!=T[j] and tmp[j]!='?':
            flag = False
            break

    if flag:
        w = S[:i] + T + S[i+len(T):]
        w = w.replace('?', 'a')
        print(w)
        exit()

print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s,t=input(),input()
ls=len(s)
lt=len(t)
ans=''
for i in range(ls-lt+1)[::-1]:
  c=0
  for j in range(lt):
    if s[i+j]==t[j] or s[i+j]=='?':
      c+=1
  if c==lt:
    ans=s[:i]+t+s[i+lt:]
    print(ans.replace('?','a'))
    break
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
list_S = list(input())
list_T = list(input())
list_S.reverse()
list_T.reverse()
num = -1
for i in range(len(list_S)):
    cnt = 0
    while True:
        if cnt == len(list_T) or i + cnt == len(list_S):
            break
        if list_S[i + cnt] == "?":
            s = list_T[cnt]
        else:
            s = list_S[i + cnt]
        if list_T[cnt] != s:
             break

        cnt += 1
    if cnt == len(list_T):
        num = i
        break

if num == -1:
    ans = "UNRESTORABLE"
else:
    for j in range(cnt):
        list_S[num+j] = list_T[j]

    for i in range(len(list_S)):
        if list_S[i] == "?":
            list_S[i] = "a"
    list_S.reverse()
    ans = "".join(list_S)
print(ans)

Submission status: Accepted

Solution code:
S=input()
T=input()
l_S=len(S)
l_T=len(T)

def matchStr(S, T, l):
  f=False
  for i in range(l):
    if S[i] != "?" and S[i] != T[i]:
      f=True
      break
  if f:
    return False
  else:
    return True

f=False
for i in range((l_S-l_T+1)-1, -1, -1): #後ろから走らないといけない（辞書式だから）
  # print(S[i:i+l_T])
  if matchStr(S[i:i+l_T], T, l_T):
    S=list(S)
    T=list(T)
    S[i:i+l_T] = T
    f=True
    break

if f:
  for i in range(l_S):
    if S[i]=="?":
      S[i] = "a"
  S="".join(S) # リストから文字列に変換
  print(S)
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import re
s = input().replace('?', '.')[::-1]
t = input()[::-1]
for i in range(len(s)-len(t)+1):
  if re.match(s[i:i+len(t)], t):
    print((s[:i].replace('.','a')+t+s[i+len(t):].replace('.','a'))[::-1])
    break
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import re
s = input().replace('?', '.')
t = input()
lst = []
for i in range(len(s)-len(t)+1):
  if re.match(s[i:i+len(t)], t):
    lst.append(s[:i].replace('.','a')+t+s[i+len(t):].replace('.','a'))
if lst:
  print(min(lst))
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import re
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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

        ls=len(S)
        lt=len(T)
        if ls<lt:
            return 'UNRESTORABLE'
        else:
            ans=[]
            S=S.replace('?','.')
            for i in range(ls-lt+1):
                m=re.match(S[i:i+lt],T)
                if m is None:
                    continue
                else:
                    ans.append((S[:i]+T+S[i+lt:]).replace('.','a'))

            if not ans:
                return 'UNRESTORABLE'
            else:
    
                return min(ans)

    print(main())








resolve()

Submission status: Accepted

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

count = 0
n = 0
ans = []
flag = 0
n=len(s)-len(t)
while n >=0:
    count = 0
    for m in range(len(t)):
        if s[n+m] == "?" or s[n+m]==t[m]:
            count += 1
        else:
            break
    if count == len(t):
        flag = 1
        break
    else:
        n -= 1

if flag == 1:
    for i in range(len(s)):
        if s[i] == "?":
            s[i] = "a"
    for i in range(m+1):
        s[n+i] =t[i]
    
    for _ in s:
        print(*_,end="")

else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input().replace('\n', '')
t=input().replace('\n', '')
begin, end = len(s) - len(t), len(s)
ans=''
while begin>=0:
    origin = s[begin:end]
    flag=1
    
    for i in range(len(t)):
        if origin[i]!=t[i] and origin[i]!='?':
            flag=0
            break
    if flag:
        ans = s[:begin]+t+s[end:]
        break
    begin-=1
    end-=1
print("UNRESTORABLE" if not len(ans) else ans.replace('?', 'a'))

Submission status: Accepted

Solution code:
Sp = input()
T = input()
S = []
for i in range(len(Sp)-len(T)+1):
	for j in range(len(T)):
		if Sp[i+j] not in ("?",T[j]):
			break
	else:
		tmp = Sp[:i] + T + Sp[i+len(T):]
		S.append(tmp.replace("?","a"))

print(min(S) if S else "UNRESTORABLE")

Submission status: Accepted

Solution code:
from copy import deepcopy


def calc_key_str(aps_S, T):
    S = deepcopy(aps_S)

    for i in range(len(aps_S) - len(T) + 1):
        matched_flag = True
        for j in range(len(T)):
            if aps_S[-i-j-1] != T[-j-1] and aps_S[-i-j-1] != '?':
                matched_flag = False
                continue

        if matched_flag:
            S_list = list(S)
            S_list[-i-len(T):len(S)-i] = T
            S = "".join(S_list)
            S = S.replace('?', 'a')
            print(S)
            break

    if not matched_flag:
        print('UNRESTORABLE')


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

    if len(aps_S) < len(T):
        print('UNRESTORABLE')
    else:
        calc_key_str(aps_S, T)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import re
s, t = input().replace('?', '.'), input()
l, m = len(s), len(t)

if l < m:
    print("UNRESTORABLE")
    exit()

can = []  # candidates
for i in range(l - m + 1):
    if re.match(s[i:i + m], t):
        can.append((s[:i] + t + s[i + m:]).replace('.', 'a'))
print(min(can) if can else "UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys

s = input()
t = input()

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

if tl > sl :
  print('UNRESTORABLE')
  sys.exit()

for i in range(sl,tl-1,-1) :
  st = s[i-tl:i]
  c = 0
  for j in range(tl) :
    if st[j] == '?' :
      continue
    elif st[j] != '?' and st[j] == t[j] :
      continue
    c = 1
    break
  if c == 0 :
    s = s[:i-tl] + t + s[i:]
    break

if c == 1 :
  print('UNRESTORABLE')
else : 
  print(s.replace('?', 'a'))

Submission status: Accepted

Solution code:
import sys

s = input()
t = input()

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

c = 1
for i in range(sl,tl-1,-1) :
  st = s[i-tl:i]
  c = 0
  for j in range(tl) :
    if st[j] == '?' :
      continue
    elif st[j] != '?' and st[j] == t[j] :
      continue
    c = 1
    break
  if c == 0 :
    s = s[:i-tl] + t + s[i:]
    break

if c == 1 :
  print('UNRESTORABLE')
else : 
  print(s.replace('?', 'a'))

Submission status: Accepted

Solution code:
from copy import deepcopy
s = list(input())
t = list(input())
lens = len(s)
lent = len(t)
ans = deepcopy(s)
for i in range(len(s)):
  if s[i] == "?":
    ans[i] = "a"
def match(moji):
  for i in range(lent):
    if moji[i] != t[i] and moji[i] != "?":
      return False
  return True
for i in range(lent, lens+1):
  ns = s[lens-i:lens-i+lent]
  if match(ns):
    ans[lens-i:lens-i+lent] = t
    print("".join(ans))
    exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

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

    S = list(S[::-1])
    T = list(T[::-1])

    flag = 0
    for i in range(0, len(S) - len(T) + 1):
        # Tが当てはまるかどうかの判定
        cnt = 0
        for j in range(len(T)):
            if S[i + j] == T[j] or S[i + j] == '?':
                cnt += 1

        if cnt == len(T):
            for j in range(len(T)):
                S[i + j] = T[j]
            flag = 1
            break

    if flag == 0:
        print('UNRESTORABLE')
        exit(0)

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

    S.reverse()
    print(''.join(S))


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)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
import re

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

if len(t)>len(s):
  print('UNRESTORABLE')
elif len(t)==len(s):
  if re.match(s, t):
    print(t)
  else:
    print('UNRESTORABLE')
else:
  for i in range(len(s)-len(t)+1):
    check_str = s[len(s)-len(t)-i:len(s)-i]
    if re.match(check_str, t):
      break

  if i==len(s)-len(t):
    print('UNRESTORABLE')
  else:
    ans = s[:len(s)-len(t)-i] + t + s[len(s)-i:]
    ans = ans.replace('.', 'a')
    print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
t = input()
ans = []
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:
        memo = s[:i] + t + s[i + len(t):]
        ans.append(memo.replace('?', 'a'))
if ans:
    print(min(ans))
    exit()
print('UNRESTORABLE')

Submission status: Accepted

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

ans = ''
if T in S:
    for i in range(len(S)):
        if S[i] == '?':
            ans += 'a'
        else:
            ans += S[i]
    print(ans)
    sys.exit()

ok_flag=False
for i in range(len(S)):
    if S[i] == '?' or S[i] == T[0]:
        for j in range(len(T)):
            if i + j > len(S) - 1:
                break
            if S[i+j] != T[j] and S[i+j] != '?':
                break
        else:
            ok_flag = True
            s_p = i

if ok_flag:
    ans = ''
    for i in range(len(S)):
        if i == s_p:
            for j in range(len(T)):
                ans += T[j]
        elif i in list(range(s_p+1,s_p+len(T))):
            continue
        elif S[i] == '?':
            ans += 'a'
        else:
            ans += S[i]
    
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

sl = len(s)
tl = len(t)
cnt = 0
ans = []
while(cnt + tl <= sl):
  tmp = 0
  for i in range(tl):
    if((s[cnt+i] == t[i]) or s[cnt+i] == '?'):
      tmp += 1
  if(tmp == tl):
    
    ans = list(s)
    ans[cnt:cnt+tl] = t
    
  cnt+=1
if(len(ans) == 0):
  print("UNRESTORABLE")
  exit()
  
for i in range(sl):
  if(ans[i] == '?'):
    ans[i] = 'a'
print("".join(ans))

Submission status: Accepted

Solution code:
import sys

s = input()
t = input()
lenS = len(s)
lenT = len(t)
ans =""
for i in range(lenS-lenT,-1,-1):
  sub = s[i:i+lenT]
  for j in range(lenT):
    if sub[j]!=t[j] and sub[j]!="?":
      break
  else:  
    ans = s[:i]+t+s[i+lenT:]
    ans = ans.replace("?","a")
    print(ans)
    sys.exit()
  continue
print("UNRESTORABLE")

Submission status: Accepted

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

flag = False
start = None
for i in range(len(S) - len(T) + 1):
    flag2 = True
    for j in range(len(T)):
        if S[i + j] == "?" or S[i + j] == T[j]:
            continue
        else:
            flag2 = False

    if flag2:
        flag = True
        start = i

if flag:
    for i in range(len(S)):
        if i < start or start + len(T) <= i:
            if S[i] == "?":
                print("a", end="")
            else:
                print(S[i], end="")
        else:
            print(T[i - start], end="")
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# import sys
# readline = sys.stdin.buffer.readline

# from collections import deque
# from copy import copy,deepcopy
# from itertools import permutations,combinations
# from pprint import pprint

# def myinput():
#     return map(int,input().split())

# def mycol(data,col):
#     return [ row[col] for row in data ]

# def mysort(data,col):
#     data.sort(key=lambda x:x[col],reverse=False)
#     return data

s = input()
t = input()
if len(s)<len(t):
    print("UNRESTORABLE")
    exit()

for i in reversed(range(len(s)-len(t)+1)):
    flag = False
    for j in range(len(t)):
        if j==(len(t)-1):
            if s[i+j]=="?":
                flag = True
            elif s[i+j]==t[j]:
                flag = True
            else:
                flag = False
        elif s[i+j]=="?":
            pass
        elif s[i+j]==t[j]:
            pass
        else:
            flag = False
            break
    if flag:
        break
    else:
        continue

if flag:
    pass
else:
    print("UNRESTORABLE")
    exit()

ans = list(s)

for l in range(len(t)):
    ans[i+l] = t[l]

ans2 = "".join(ans)

# if "?" in ans2:
ans3 = ans2.replace("?","a")
print(ans3)

Submission status: Accepted

Solution code:
S=input()
T=input()
flag=1
buf=""
ans=""
sus=[]
piv=0
if len(S)<len(T):
    print("UNRESTORABLE")
else:
    for i in range(len(S)-len(T)+1):
        for j in range(len(T)):
            #print(S[i+j],T[j])
            if (S[i+j]!="?")and(T[j]!=S[i+j]):
                flag=0
                break
            #input()
        if flag==1:
            sus.append(i)
        flag=1
    if len(sus)<1:
        print("UNRESTORABLE")
    else:
        piv=max(sus)
        for i in range(len(S)):
            if piv<=i<=piv+len(T)-1:
                buf="".join((buf,T[i-piv]))
            else:
                buf = "".join((buf, S[i]))
        #print(buf)
        for i in range(len(S)):
            if buf[i]=="?":
                ans="".join((ans,"a"))
            else:
                ans = "".join((ans, buf[i]))
        print(ans)

Submission status: Accepted

Solution code:
import sys
S = input().strip()
T = input().strip()


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


ans = "UNRESTORABLE"
for i in range(len(S)-len(T), -1, -1):
    s = S[i:i+len(T)]
    if comp(s, T):
        S = S[:i] + T + S[i+len(T):]
        for i in range(len(S)):
            if S[i] == "?":
                S = S[:i] + "a" + S[i+1:]
        print(S)
        sys.exit(0)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    S = Scanner.string()
    T = Scanner.string()
    N = len(S)
    M = set()
    for i in range(N - len(T) + 1):
        flag = True
        for j in range(len(T)):
            if not(S[i + j] == T[j] or S[i + j] == '?'):
                flag = False
        if flag:
            A = S[:i] + T + S[i + len(T):]
            A = ['a' if s == '?' else s for s in A]
            M.add(''.join(A))
    if len(M) > 0:
        print(sorted(list(M))[0])
    else:
        print('UNRESTORABLE')


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

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

res = 'UNRESTORABLE'

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

flg = False

for i in range(len(s)-len(t), -1, -1):
	L = len(t)
	sub = s[i:L+i]
	if check(sub,t):
		new = s[:i] + t + s[L+i:]
		flg = True
		break


if flg:
	print(new.replace('?', 'a'))
else:
	print(res)

Submission status: Accepted

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

ans = ""
if len(T) > len(S):
    print("UNRESTORABLE")
else:
    ls = len(S)
    lt = len(T)
    for i in reversed(range(ls-lt+1)):
        tmp = S[i:i+lt]
        flag = True
        for j in range(lt):
            if tmp[j] != "?":
                if tmp[j] != T[j]:
                    flag = False
                    break
        if flag:
            ans = ""
            for h in range(i):
                if S[h] == "?":
                    ans += "a"
                else:
                    ans += S[h]
            ans += T
            for h in range(i+lt,ls):
                if S[h] == "?":
                    ans += "a"
                else:
                    ans += S[h]
            print(ans)
            exit()
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# _S = input()
# T = input()

# _S = list("?tc????")
# T = list("coder")


# _S = list("??p??d??")
# T = list("abc")

_S = list(input())
T = list(input())

def calculate(_S,T):
    _sl = len(_S)
    tl = len(T)

    result = []
    for startIndex in range(0,_sl - tl + 1):

        isOk = True
        for j in range(tl):
            _SValue = _S[startIndex+j]
            if (_SValue != "?") and (_SValue != T[j]):
                isOk = False
                break


        if isOk:
            copyS = _S.copy()
            copyS[startIndex:startIndex + tl] = T
            res = "".join(copyS).replace("?","a")
            result.append(res)


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

calculate(_S,T)