Problem: You are given a string S consisting of lowercase English letters. Another string T is initially empty. Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:    * Append one of the following at the end of T: `dream`, `dreamer`, `erase` and `eraser`.

Submission attempts (in order):
from collections import deque

def solve(s):
    que = deque()
    que.append(s)
    while len(que) > 0:
        top = que.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                que.append(s[:7])
            que.append(s[:5])
        elif top[:5] == 'erase':
            if top[5] == 'r':
                que.append(top[:6])
            else:
                que.append(top[:5])
    return 'NO'

print(solve(input()))


from collections import deque

def solve(s):
    stack = deque()
    stack.append(s)
    while len(stack) > 0:
        top = stack.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                stack.append(top[7:])
            stack.append(top[:5])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                stack.append(top[6:])
            else:
                stack.append(top[5:])
    return 'NO'

print(solve(input()))


s = input()
while s != '':
    if s[:5] == 'dream' or s[:5] == 'erase':
        if s[5] == 'r':
            s = s[6:]
        else:
            s = s[5:]
    else:
        print('NO')
        quit(0)

print('YES')


s = input()
while s != '':
    if s[:5] == 'dream' or s[:5] == 'erase':
        if s[5] = 'r':
            s = s[6:]
        else:
            s = s[5:]
    else:
        print('NO')
        quit(0)

print('YES')


from collections import deque

def solve(s):
    que = deque()
    que.append(s)
    while len(que) > 0:
        top = que.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                que.append(s[:7])
            que.append(s[:5])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                que.append(top[:6])
            else:
                que.append(top[:5])
    return 'NO'

print(solve(input()))


from collections import deque

def solve(s):
    que = deque()
    que.append(s)
    while len(que) > 0:
        top = que.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                que.append(top[:7])
            que.append(top[:5])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                que.append(top[:6])
            else:
                que.append(top[:5])
    return 'NO'

print(solve(input()))


from collections import deque

def solve(s):
    stack = deque()
    stack.append(s)
    while len(stack) > 0:
        top = stack.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                stack.append(top[7:])
            stack.append(top[5:])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                stack.append(top[6:])
            else:
                stack.append(top[5:])
    return 'NO'

print(solve(input()))


Verdicts: Runtime Error

Time Limit Exceeded

Runtime Error

Runtime Error

Time Limit Exceeded

Time Limit Exceeded

Accepted