Problem p03565 (python)

Fixed submission:
S_ = input()
T = input()
ans = list(S_)


def check(S1, S2):
    for i in range(len(S1)):
        if S1[i] == S2[i] or S1[i] == "?":
            continue
        else:
            return(False)

    return(True)


for i in reversed(range(len(S_) - len(T) + 1)):
    result = check(S_[i:i+len(T)], T)
    if result:
        for j in range(len(T)):
            ans[i+j] = T[j]
        for j in range(len(ans)):
            if ans[j] == "?":
                ans[j] = "a"
        print("".join(ans))

        exit(0)

print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
S_ = input()
T = input()
ans = list(S_)
for i in range(len(T)):
    if T[i] in S_:
        prev = len(S_)
        while(1):
            id = S_.rfind(T[i], 0, prev-1)
            if id < 0:
                break
            prev = id
            flag = True
            for j in range(i+1, len(T)):
                if S_[id + j - i] == T[i] or S_[id + j - i] == "?":
                    continue
                else:
                    flag = False
                    break
                for j in reversed(range(i)):
                    if S_[id - j - 1] == T[i - j - 1] or S_[id - j - 1] == "?":
                        continue
                    else:
                        flag = False
                        break
            if flag:
                ans[id - i:id - i + len(T)] = T
                for i in range(len(ans)):
                    if ans[i] == "?":
                        ans[i] = "a"
                print("".join(ans))
                exit(0)


print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
S_ = input()
T = input()
ans = list(S_)
for i in range(len(T)):
    if T[i] in S_:
        prev = len(S_)
        while(1):
            id = S_.rfind(T[i], 0, prev-1)
            if id < 0:
                break
			prev = id
            flag = True
            for j in range(i+1, len(T)):
                if S_[id + j - i] == T[i] or S_[id + j - i] == "?":
                    continue
                else:
                    flag = False
                    break
                for j in reversed(range(i)):
                    if S_[id - j - 1] == T[i - j - 1] or S_[id - j - 1] == "?":
                        continue
                    else:
                        flag = False
                        break
            if flag:
                ans[id - i:id - i + len(T)] = T
                for i in range(len(ans)):
                    if ans[i] == "?":
                        ans[i] = "a"
                print("".join(ans))
                exit(0)


print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
S_ = input()
T = input()
ans = list(S_)
for i in range(len(T)):
    if T[i] in S_:
        prev = len(S_)
        while(1):
            id = S_.rfind(T[i], 0, prev-1)
            if id < 0:
                break

            flag = True
            for j in range(i+1, len(T)):
                if S_[id + j - i] == T[i] or S_[id + j - i] == "?":
                    continue
                else:
                    flag = False
                    break
                for j in reversed(range(i)):
                    if S_[id - j - 1] == T[i - j - 1] or S_[id - j - 1] == "?":
                        continue
                    else:
                        flag = False
                        break
            if flag:
                ans[id - i:id - i + len(T)] = T
                for i in range(len(ans)):
                    if ans[i] == "?":
                        ans[i] = "a"
                print("".join(ans))
                exit(0)


print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
S_ = input()
T = input()
ans = list(S_)
for i in range(len(T)):
    if T[i] in S_:
        id = S_.rfind(T[i])
        if id < 0:
            continue
        flag = True
        for j in range(i+1, len(T)):
            if S_[id + j - i] == T[i] or S_[id + j - i] == "?":
                continue
            else:
                flag = False
                break
            for j in reversed(range(i)):
                if S_[id - j - 1] == T[i - j - 1] or S_[id - j - 1] == "?":
                    continue
                else:
                    flag = False
                    break
        if flag:
            ans[id - i:id - i + len(T)] = T
            for i in range(len(ans)):
                if ans[i] == "?":
                    ans[i] = "a"
            print("".join(ans))
            exit(0)


print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
S_ = input()
T = input()
ans = list(S_)
for i in range(len(T)):
    if T[i] in S_:
        id = S_.rfind(T[i])
        flag = True
        for j in range(i+1, len(T)):
            if S_[id + j - i] == T[i] or S_[id + j - i] == "?":
                continue
            else:
                flag = False
                break
            for j in reversed(range(i)):
                if S_[id - j - 1] == T[i - j - 1] or S_[id - j - 1] == "?":
                    continue
                else:
                    flag = False
                    break
        if flag:
            ans[id - i:id - i + len(T)] = T
            for i in range(len(ans)):
                if ans[i] == "?":
                    ans[i] = "a"
            print("".join(ans))
            exit(0)


print("UNRESTORABLE")