Problem:
 Score : 300 points    Problem Statement 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 .       Constraints  1≦|S|≦10^5  S consists of lowercase English letters.         Input The input is given from Standard Input in the following format:  S       Output If it is possible to obtain S = T , print YES . Otherwise, print NO .        Sample Input 1 erasedream      Sample Output 1 YES  Append erase and dream at the end of T in this order, to obtain S = T .       Sample Input 2 dreameraser      Sample Output 2 YES  Append dream and eraser at the end of T in this order, to obtain S = T .       Sample Input 3 dreamerer      Sample Output 3 NO

Solution (Python):
S = input()
while len(S) > 0:
    if S[-5:] == 'dream' or S[-5:] == 'erase':
        S = S[:-5]
    elif S[-6:] == 'eraser':
        S = S[:-6]
    elif S[-7:] == 'dreamer':
        S = S[:-7]
    else:
        break
if len(S) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
while len(S) > 0:
    if S[-5:] == 'dream' or S[-5:] == 'erase':
        S = S[:-5]
    elif S[-6:] == 'eraser':
        S = S[:-6]
    elif S[-7:] == 'dreamer':
        S = S[:-7]
    else:
        break
if len(S) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
S=input()
S1 = S.replace('erase', 'YYY').replace('YYYr', '').replace('YYY', '').replace('dreamer', '').replace('dream', '')
if S1 == '':
    print('YES')
else:
    print('NO')

Solution (Python):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$",input()) else "NO")

Solution (Python):
def DFS(S):
    word_list = [ "dreamer","dream", "eraser", "erase"]
    stack = ["dream", "dreamer", "erase", "eraser"]
    while stack:
        word = stack.pop()
        if word == S[0]:
            return "YES"
        for i in word_list:
            next_word = word + i
            check = S[0][:len(word + i)]
            if next_word != check:
                continue
            if next_word == check:
                stack.append(next_word)
    return "NO"






if __name__ == "__main__":
    S = [input()]
    print(DFS(S))

Solution (Python):
def DFS(S):
    word_list = ["dreamer", "dream", "eraser", "erase"]
    stack = ["dream", "dreamer", "erase", "eraser"]
    while stack:
        word = stack.pop()
        if word == S[0]:
            return "YES"
        for i in word_list:
            next_word = word + i
            check = S[0][:len(word + i)]
            if next_word != check:
                continue
            if next_word == check:
                stack.append(next_word)
    return "NO"
 
 
 
 
 
 
if __name__ == "__main__":
    S = [input()]
    print(DFS(S))

Solution (Python):
S = input()

dp = [False] * (len(S) + 1)
dp[0] = True
T = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(1, len(S) + 1):
    tmp = []
    for t in T:
        if i < len(t):
            continue
        tmp.append(dp[i-len(t)] and t == S[i-len(t): i])
    if any(tmp):
        dp[i] = True
print('YES' if dp[len(S)] else 'NO')

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

S = S.replace('eraser', '')
S = S.replace('erase', '')
S = S.replace('dreamer', '')
S = S.replace('dream', '')

if len(S) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
import queue


def check_dream(s):
    return s.startswith('dream')


def check_dreamer(s):
    return s.startswith('dreamer')


def check_erase(s):
    return s.startswith('erase')


def check_eraser(s):
    return s.startswith('eraser')


def check_sentence(s, q):
    if check_dream(s):
        q.put(s[5:])

    if check_dreamer(s):
        q.put(s[7:])

    if check_erase(s):
        q.put(s[5:])

    if check_eraser(s):
        q.put(s[6:])

    return q


def search():
    sentence_q = queue.Queue()
    initial_s = input()

    s = initial_s
    while(1):
        if s == '':
            return True

        sentence_q = check_sentence(s, sentence_q)
        if sentence_q.empty():
            return False

        s = sentence_q.get()


if search():
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()

s = s.replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')

if s == '':
    print("YES")
else:
    print("NO")

Solution (Python):
import sys


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


print(
    "NO"
    if ii(str)
    .replace("eraser", "")
    .replace("erase", "")
    .replace("dreamer", "")
    .replace("dream", "")
    else "YES"
)

Solution (Python):

s=input().replace("eraser","").replace('erase',"").replace("dreamer","").replace("dream","")
print("YES" if s=="" else "NO")

Solution (Python):
a=input()
a=a.replace("eraser","")
a=a.replace("erase","")
a=a.replace("dreamer","")

a=a.replace("dream","")
if a=="":
  print("YES")
else:
  print("NO")

Solution (Python):
s = input().replace("eraser", "").replace("erase", "").replace("dreamer", "").replace("dream", "")
if s:
  print("NO")
else:
  print("YES")

Solution (Python):
import sys
S = input()
S = S[::-1]

AR = []
A = ["dream","dreamer","erase","eraser"]
for a in A:
    AR.append(a[::-1])

i = 0
while i < len(S):
    for ar in AR:
        l = len(ar)
        if ar == S[i:i+l]:
            i += l
            break
    else:
        print("NO")
        sys.exit()

print("YES")

Solution (Python):
s = input()
s = s[::-1]
part = ['dream'[::-1],'dreamer'[::-1],'erase'[::-1],'eraser'[::-1]]
i = 0
while(1):
    ok = False
    for p in part:
        if s[i:i+len(p)] == p:
            i += len(p)
            ok = True
            break
    if ok == False or i == len(s):
        break
print('YES' if ok else 'NO')

Solution (Python):
S = input()[::-1]

words = list(map(lambda s: s[::-1], ['dreamer', 'eraser', 'erase', 'dream']))

while True:
    for w in words:
        if S[:len(w)] == w:
            S = S[len(w):]
            break
    else:
        break

print('YES' if len(S) == 0 else 'NO')

Solution (Python):
d = ["dream", "dreamer", "erase", "eraser"]

def getDevidedString(s):
    i = 0
    while i < 4:
        position = len(d[i]) * -1
        str = s[position:]
        if str == d[i]:
           return s[0:position]
        i += 1
    return s

S = input()

while len(S) != 0:
    tmp = getDevidedString(S)
    if len(S) != len(tmp):
        S = tmp
    else:
        break

if len(S)== 0:
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
s = s[::-1]
while len(s)>0:
    if s[0:5] == 'dream'[::-1]:
        s = s[5:]
    elif s[0:7] == 'dreamer'[::-1]:
        s = s[7:]
    elif s[0:5] == 'erase'[::-1]:
        s = s[5:]
    elif s[0:6] == 'eraser'[::-1]:
        s = s[6:]
    else:
        print('NO')
        exit()

print('YES')

Solution (Python):


S=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")


if S:
    print("NO")
else:
    print("YES")

Solution (Python):
S=input()[::-1]
i=0
while i<len(S):
    if S[i:i+6]=="resare":
        i+=6
    elif S[i:i+5]=="esare":
        i+=5
    elif S[i:i+7]=="remaerd":
        i+=7
    elif S[i:i+5]=="maerd":
        i+=5
    else:
        print("NO")
        break
else:
    print("YES")

Solution (Python):


def main():
    s = input()
    word_list = ['dream', 'dreamer', 'erase', 'eraser']

    i = len(s)
    while 0 < i:
        f = False
        for w in word_list:
            if s[i - len(w):i] == w:
                i -= len(w)
                f = True
                break
        if not f:
            break

    print('YES' if f else 'NO')


if __name__ == '__main__':
    main()

Solution (Python):
s = input()

bool = [False]

def hantei(s):
    
    if s=='':
        bool[0]=True
        return

    if s.count('dream')>=1:
        r=s.replace('dream','')
        hantei(r)

    if s.count('dreamer')>=1:
        r=s.replace('dreamer','')
        hantei(r)

    if s.count('erase')>=1:
        r=s.replace('erase','')
        hantei(r)

    if s.count('eraser')>=1:
        r=s.replace('eraser','')
        hantei(r)
    return

hantei(s)
        

if bool[0]==True:
    print('YES')

if bool[0]==False:
    print('NO')

Solution (Python):
strings = ['eraser','erase', 'dreamer','dream' ]


S = str(input())


for s in strings:
    S = S.replace(s, "")

if S == "":
    print('YES')
else:
    print("NO")

Solution (Python):
S = input()

cnt = 0
j = len(S) - 1

for i in range(len(S) - 1, -1, -1):
    if j - i > len("dreamer"):
        break
    for dd in ("dream", "dreamer", "erase", "eraser"):
        if S[i:j + 1] == dd:
            j = i - 1
            cnt += len(dd)

print(("NO", "YES")[len(S) == cnt])

Solution (Python):
S = input()

partstrs = ["eraser", "erase", "dreamer", "dream"]

for s in partstrs:
    S = S.replace(s , "") 
if S=="":
    print("YES")
else:
    print("NO")

Solution (Python):
print("YES" if input().replace("eraser", "").replace("erase", "").replace("dreamer", "").replace("dream", "")=="" else "NO")

Solution (Python):
S = input()
S = S.replace("eraser",'').replace("erase",'').replace("dreamer",'').replace("dream",'')
if(S == ''):
  print("YES")
else:
  print("NO")

Solution (Python):
def main():
    s = input()
    while s:
        if s[-5:] == 'dream' or s[-5:] == 'erase':
            s = s[:-5]
        elif s[-6:] == 'eraser':
            s = s[:-6]
        elif s[-7:] == 'dreamer':
            s = s[:-7]
        else:
            break

    if s:
        print('NO')
    else:
        print('YES')


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
i = 0
n = len(s)
p = 0
while i < n:
    if i == 0:
        if s[i] == "d":
            if i + 5 <= n:
                if s[i:i+5] == "dream":
                    i = i + 5
                    if i + 5 < n:
                        if s[i] != "e" and s[i] != "d":
                            print("NO")
                            exit()
                else:
                    print("NO")
                    exit()
            else:
                print("NO")
                exit()
        elif s[i] == "e":
            if i + 2 <= n:
                if s[i:i+2] == "er":
                    i = i + 1
                else:
                    print("NO")
                    exit()
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()
    else:
        if s[i] == "d":
            if i + 5 <= n:
                if s[i:i+5] == "dream":
                    i = i + 5
                    if i + 5 < n:
                        if s[i] != "e" and s[i] != "d":
                            print("NO")
                            exit()
                else:
                    print("NO")
                    exit()
            else:
                print("NO")
                exit()
        elif s[i] == "e":
            if i + 2 <= n:
                if s[i:i+2] == "er":
                    i = i + 1
                else:
                    print("NO")
                    exit()
            else:
                print("NO")
                exit()
        elif s[i] == "r":
            if s[i - 5: i + 1] == "eraser" and i - 5 >= 0:
                if s[i - 2] != "r":
                    if i + 1 < n:
                        if s[i + 1] == "d":
                            i = i + 1
                        elif s[i + 1] == "e":
                            i = i + 1
                        else:
                            print("NO")
                            exit()
                        
                    else:
                        print("YES")
                        exit()
            elif i + 4 <= n:
                if s[i - 2] != "r":
                    if i + 1 < n:
                        if s[i + 1] == "d":
                            i = i + 1
                            p = 1
                        elif s[i + 1] == "e":
                            i = i + 1
                            p = 1
                    else:
                        print("YES")
                        exit()
                if s[i:i+4] == "rase":
                    i = i + 4
                    p = 1
                if p == 0:
                    print("NO")
                    exit()
            elif s[i - 2] != "r":
                if i + 1 < n:
                    if s[i + 1] == "d":
                        i = i + 1
                    elif s[i + 1] == "e":
                        i = i + 1
                    else:
                        print("NO")
                        exit()
                else:
                    print("YES")
                    exit()
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()
    p = 0
print("YES")

Solution (Python):
s = input()
str_list = ["eraser","erase","dreamer","dream"]
for e in str_list:
    s = s.replace(e,"
s = s.replace("
ans = "YES" if s == "" else "NO"
print(ans)

Solution (Python):
s = input()
str_list = ["eraser","erase","dreamer","dream"]
for e in str_list:
    s = s.replace(e,"")
ans = "YES" if s == "" else "NO"
print(ans)

Solution (Python):
s = input()
for i in range(len(s) // 5):
    if s[-5:] == "dream" or s[-5:] == "erase":
        s = s[:-5]
    elif s[-6:] == "eraser":
        s = s[:-6]
    elif s[-7:] == "dreamer":
        s = s[:-7]
ans = "YES" if s == "" else "NO"
print(ans)

Solution (Python):
s = input()
for i in range(len(s) // 5):
    if s[-7:] == "dreamer":
        s = s[:-7]
    elif s[-6:] == "eraser":
        s = s[:-6]
    elif s[-5:] == "dream" or s[-5:] == "erase":
        s = s[:-5]
ans = "YES" if s == "" else "NO"
print(ans)

Solution (Python):
s = input()
str_list = ["dream","dreamer","erase","eraser"]
for i in range(len(s) // 5):
    if s[-7:] == "dreamer":
        s = s[:-7]
    elif s[-6:] == "eraser":
        s = s[:-6]
    elif s[-5:] == "dream" or s[-5:] == "erase":
        s = s[:-5]
ans = "YES" if s == "" else "NO"
print(ans)

Solution (Python):
S = input()
s = S[::-1]

word = ['maerd', 'remaerd', 'esare', 'resare']

T = []

now = 0
for i in range(10**5):
    if s[now:now+5] == 'maerd':
        now += 5
    elif s[now:now+5] == 'esare':
        now += 5
    elif s[now:now+7] == 'remaerd':
        now += 7
    elif s[now:now+6] == 'resare':
        now += 6
    else:
        break

if len(S) == now:
    print('YES')
else:
    print('NO')

Solution (Python):
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0 for _ in range(len(s)+1)]
ok = False
while len(stack) > 0:
    start_id = stack.pop()
    string = s[start_id:]
    if start_id == len(s):
        print('YES')
        ok = True
        break

    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            if next_start_id <= len(s):
                part = string[next_start_id:]
                if mem[next_start_id] == 0:
                    stack.append(next_start_id)
                    mem[next_start_id] = 1
if not ok:
    print('NO')

Solution (Python):
S = input().strip()

while True:
  if S.endswith('dream'):
    S = S[:-5]
  elif S.endswith('dreamer'):
    S = S[:-7]
  elif S.endswith('erase'):
    S = S[:-5]
  elif S.endswith('eraser'):
    S = S[:-6]
  else:
    break

if S:
  print("NO")
else:
  print('YES')

Solution (Python):
S = input()
L = ['dream', 'dreamer', 'erase', 'eraser']

cnt=0 
while len(S):
  for item in L:
    if S[-1*len(item):] == item:
      S = S[:-1*len(item)]
  cnt+=1
  if len(S) == 0:
    print("YES")
  if cnt > 100001:
    print("NO")
    exit(0)

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):

S = input()

words = S.replace("d", " d").lstrip().split()

ans = "NO"
for word in words:
    word_tmp = word
    while len(word_tmp) > 0:
        if word_tmp[-6:] == "eraser":
            word_tmp = word_tmp[:-6]
        elif word_tmp[-5:] == "erase":
            word_tmp = word_tmp[:-5]
        elif word_tmp[-7:] == "dreamer":
            word_tmp = word_tmp[:-7]
        elif word_tmp[-5:] == "dream":
            word_tmp = word_tmp[:-5]
        else: break
    else:
        continue
    break
else: ans = "YES"

print(ans)

Solution (Python):
s = input().replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')
if s:
    print('NO')
else:
    print('YES')

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
S = input()[::-1]
List = ["maerd", "remaerd", "esare", "resare"]
while True:
    if len(S) == 0:
          print("YES")
          break
    if S[0:7] in List:
        S = S[7:]
    elif S[0:6] in List:
        S = S[6:]
    elif S[0:5] in List:
        S = S[5:]
    else:
        print("NO")
        break

Solution (Python):
s = input()
size = len(s)

dp = [False] * (size + 1)
dp[0] = True

for i in range(5, size + 1):
    if dp[i - 5] and (s[i-5:i] == 'dream' or s[i-5:i] == 'erase'):
        dp[i] = True
    elif dp[i - 6] and s[i-6:i] == 'eraser':
        dp[i] = True
    elif dp[i - 7] and s[i-7:i] == 'dreamer':
        dp[i] = True
    else:
        dp[i] = False
if dp[size]:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()


while len(s) > 0:
    if s[-6:] == 'eraser':
        s=s[:-6]
    elif s[-7:] == 'dreamer':
        s=s[:-7]
    elif s[-5:] == 'erase' or s[-5:] == 'dream':
        s=s[:-5]
    else:
        print('NO')
        exit(0)
print('YES')

Solution (Python):
import sys
S = input()
while S:
    if (S[-5:] == 'dream') or (S[-5:] == 'erase'):
        S = S[:-5]
    elif S[-6:] == 'eraser':
        S = S[:-6]
    elif S[-7:] == 'dreamer':
        S = S[:-7]
    else:
        print('NO')
        sys.exit()
print('YES')

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
s = input()

ichi = 0
l = len(s)
cnt = l

while True:
    s_dreamer = s.rfind ("dreamer",ichi,l)
    s_eraser = s.rfind ("eraser",ichi,l)
    s_dream = s.rfind ("dream",ichi,l)
    s_erase = s.rfind ("erase",ichi,l)

    if s_dreamer == l - 7 and 0 <= l - 7:
        l = s_dreamer
        cnt -= 7 

    elif s_eraser == l - 6 and 0 <= l - 6:
        l = s_eraser
        cnt -= 6  

    elif s_dream == l - 5 and 0 <= l - 5:
        l = s_dream
        cnt -= 5  

    elif s_erase == l - 5 and 0 <= l - 5:
        l = s_erase
        cnt -= 5

    elif cnt == 0:
        print("YES")
        break

    else:
        print("NO")
        break

Solution (Python):
s=input()

s=s.replace("eraser","")
s=s.replace("erase","")
s=s.replace("dreamer","")
s=s.replace("dream","")

if s=="":
    print("YES")
else:
    print("NO")

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
S = input()
def func(i):
    if i >= len(S):
        return True
    if (S[i:i+5] == 'dream' or S[i:i+5] == 'erase') and func(i + 5):
        return True
    if (S[i:i+7] == 'dreamer') and func(i + 7):
        return True
    if (S[i:i+6] == 'eraser') and func(i + 6):
        return True
    return False

print('YES' if func(0) else 'NO')

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

S = input()

def func(i):
    if i >= len(S):
        return True
    if S[i:i+5] == 'dream':
        if func(i + 5): return True
        if S[i+5:i+7] == 'er':
            if func(i + 7): return True
        return False
    if S[i:i+5] == 'erase':
        if func(i + 5): return True
        if S[i+5] == 'r':
            if func(i + 6): return True
        return False

print('YES' if func(0) else 'NO')

Solution (Python):
s=input()
ind =0
len_s=len(s)
s = s+ "AAAAAAAAA"

A = "dreamer"
B= "dream"
C= "erase"
D="eraser"

flag =True
while(1):
    if(s[ind] =="d"):
        if(s[ind + 5] =="d"):
            if(s[ind:ind+5]!=B):
                flag = False
                break
            else:
                ind+=5
        else:
            if(s[ind+5]!='e' and s[ind+5]!='A'):
                flag=False
                break
            if(s[ind+7] == "a" or ind + 7 >len_s):
                if(s[ind:ind+5]!=B):
                    flag = False
                    break
                else:
                    ind+=5

            else:
                if(s[ind:ind+7] !=A):
                    flag = False
                    break
                else:
                    ind+=7
    elif(s[ind]=="e"):
        if(s[ind+5]=="r"):
            if(s[ind:ind+6]!=D):
                flag = False
                break
            else:
                ind+=6

        else:
            if(s[ind:ind +5] !=C):
                flag =False
                break
            else:
                ind+=5
    else:
        flag=False
        break
    if(ind == len_s):
        break
    if(ind > len_s):
        flag=False
        break
if(flag):
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
s += "0000000000"
i = 0
while i < len(s):
    
    if s[i:i + 5] == "dream":
        if s[i + 5:i + 7] == "er":
            if s[i + 7] == "a":  
                i += 5
                continue
            else:  
                i += 7
                continue
        else:  
            i += 5
            continue
    elif s[i:i+5] == "erase":  
        if s[i + 5] != "r":  
            i += 5
            continue
        else:  
            i += 6
            continue
    elif s[i:] == "0000000000":
        print("YES")
        exit()
    else:
        print("NO")
        exit()

Solution (Python):


def check(string):
    if string[-5:] == "dream":
        new = string[:-5]
        return new

    if string[-7:] == "dreamer":
        new = string[:-7]
        return new

    if string[-5:] == "erase":
        new = string[:-5]
        return new

    if string[-6:] == "eraser":
        new = string[:-6]
        return new

    print("NO")


if __name__ == '__main__':
    s = str(input())

    while s:
        s = check(s)

    if s == "":
        print("YES")

Solution (Python):
S = input()
S = S[::-1]
check = ['maerd', 'remaerd', 'esare', 'resare']
T = []
now = 0

while now < len(S):
    if S[now:now+5] == 'maerd':
        now += 5
        T.append('maerd')
    elif S[now:now+5] == 'esare':
        now += 5
        T.append('esare')
    elif S[now:now+7] == 'remaerd':
        now += 7
        T.append('remaerd')
    elif S[now:now+6] == 'resare':
        now += 6
        T.append('resare')
    else:
        print('NO')
        break
if ''.join(T) == S:
    print('YES')

Solution (Python):
s = input().replace("eraser", "").replace("erase", "").replace("dreamer", "").replace("dream", "")
if s:
  print("NO")
else:
  print("YES")

Solution (Python):
s = input()
words = ['dream', 'dreamer', 'erase', 'eraser']
r = len(s)
t = ''

for l in range(len(s), -1, -1):
  if r - l >= 5:
    if s[l:r] in words:
      t = s[l:r] + t
      r = l

if s == t:
  print('YES')
else:
  print('NO')

Solution (Python):
import copy

S = input()
strs = list(S)
strs.reverse()
targets = [list("maerd"), list("remaerd"), list("esare"), list("resare")]

cnt = -1
_targets = copy.copy(targets)
for i in range(len(strs)):
    s = strs[i]
    cnt += 1
    is_match = False
    __targets = copy.copy(_targets)
    for target in __targets:
        if len(target) <= cnt:
            continue
        if target[cnt] == s:
            is_match = True
            if len(target) - 1 == cnt:
                _targets = copy.copy(targets)
                cnt = -1
            break
        else:
            _targets.remove(target)
    if not(is_match):
        print("NO")
        exit()

print("YES")

Solution (Python):
A = ['dream', 'dreamer', 'erase', 'eraser']

for i in range(len(A)):
    A[i] = A[i][::-1]

s = input()[::-1]
i = 0
while True:
    f = True
    for a in A:
        if len(a) > len(s) - i:
            continue
        if s[i:i+len(a)] == a:
            i += len(a)
            f = False
            break
    else:
        if i == len(s):
            print('YES')
            exit()
        if f == True:
            print('NO')
            exit()


print('YES')

Solution (Python):
s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
 
if s:
  print("NO")
else:
  print("YES")

Solution (Python):
T = ["dream", "dreamer","erase", "eraser", ""]
S = input()
L = len(S)
ans = "YES"
while L >= 7:
    if S[-7:] == "dreamer":
        L -= 7
        S = S[:-7]
    elif S[-6:] == "eraser":
        L -= 6
        S = S[:-6]
    elif S[-5:] == "erase" or S[-5:] == "dream":
        L -= 5
        S = S[:-5]
    else:
        ans = "NO"
        break
if S in T:
    print(ans)
else:
    print("NO")

Solution (Python):
S=str(input())
for i in range(20000):
    if S[-7:]=="dreamer":
        S=S[:-7]
    if S[-6:]=="eraser":
        S=S[:-6]
    if S[-5:]=="dream" or S[-5:]=="erase":
        S=S[:-5]
    if S[-7:]!="dreamer" and S[-6:]!="eraser" and S[-5:]!="dream" and S[-5:]!="erase":
        break
if len(S)==0:
    print("YES")
else:
    print("NO")

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
print(["NO","YES"][len(s)==0])

Solution (Python):
s=input()
s=s.replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if len(s)==0:
    print('YES')
else:
    print('NO')

Solution (Python):

S = input()
S = S.replace("eraser", "").replace("erase", "").replace("dreamer", "").replace("dream", "")
print("YES" if len(S)==0 else "NO")

Solution (Python):
import sys
S = input()
S = S[::-1]
while S:
    if S[:3]=="res":
        if S[:6]=="resare":
            S = S[6:]

        else:
            break

    elif S[:3]=="esa":
        if S[:5]=="esare":
            S = S[5:]

        else:
            break
    
    elif S[:3]=="rem":
        if S[:7]=="remaerd":
            S = S[7:]

        else:
            break
            
    elif S[:3]=="mae":
        if S[:5]=="maerd":
            S = S[5:]

        else:
            break
            
    else:
        break

print("YES" if len(S)==0 else "NO")

Solution (Python):
import sys
S = input()
S = S[::-1]
while S:
    if S[:3]=="res":
        if S[:6]=="resare":
            S = S[6:]

        else:
            print("NO")
            sys.exit()

    elif S[:3]=="esa":
        if S[:5]=="esare":
            S = S[5:]

        else:
            print("NO")
            sys.exit()
    
    elif S[:3]=="rem":
        if S[:7]=="remaerd":
            S = S[7:]

        else:
            print("NO")
            sys.exit()
        
    elif S[:3]=="mae":
        if S[:5]=="maerd":
            S = S[5:]

        else:
            print("NO")
            sys.exit()
            
    else:
        print("NO")
        sys.exit()
else:
    print("YES")

Solution (Python):
s = input()

while len(s) > 4:
    if s[-5:] == 'dream' or s[-5:] == 'erase':
        s = s[:-5]
        continue
    elif s[-6:] == 'eraser':
        s = s[:-6]
        continue
    elif s[-7:] == 'dreamer':
        s = s[:-7]
        continue
    else:
        break
        
if len(s) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")

if s:
  print("NO")
else:
  print("YES")

Solution (Python):



import sys


def main():
    line = sys.stdin.readline().strip()
    line1 = line.replace('eraser', '').replace(
        'erase', '').replace('dreamer', '').replace('dream', '')
    if line1 == '':
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Solution (Python):


s = input()
t = ["eraser", "erase", "dreamer", "dream"]
for n in range(4):
    s = s.replace(t[n], "")
if not s:
    print('YES')
else:
    print('NO')

Solution (Python):
S = "".join(list(reversed(list(input()))))

flag = 0
while flag == 0 :
    if (S[0:7] == "remaerd") and (len(S) >= 7) :
        S = S[7:]
    elif (S[0:6] == "resare" and len(S) >= 6) :
        S = S[6:]
    elif ((S[0:5] == "maerd") or (S[0:5] == "esare")) and (len(S) >= 5) :
        S = S[5:]
    elif len(S) == 0 :
        ans = 1
        flag = 1
    else :
        ans = -1
        flag = 1
D = {1:"YES",-1:"NO"}
print(D[ans])

Solution (Python):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$", input()) else "NO")

Solution (Python):
s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")

if s :
    print("NO")
else:
    print("YES")

Solution (Python):
S = input()
Scopy = S[::-1]
check = ['maerd', 'remaerd', 'esare', 'resare']
now = 0
while now < len(S):
    if Scopy[now] == 'm':
        if Scopy[now:now+5] != check[0]:
            print('NO')
            break
        else:
            now += 5
    
    elif Scopy[now] == 'e':
        if Scopy[now:now+5] != check[2]:
            print('NO')
            break
        else:
             now += 5
    
    elif Scopy[now] == 'r':
        if Scopy[now:now+7] == check[1]:
            now += 7
        elif Scopy[now:now+6] == check[3]:
            now += 6
        else:
            print('NO')
            break
    else:
        print('NO')
        break

if now == len(S):
    print('YES')

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

S = S[::-1]

words = ('dreamer', 'dream', 'eraser', 'erase')
sdrow = [word[::-1] for word in words]

while S:
    flag = False
    for drow in sdrow:
        if S.startswith(drow):
            flag = True
            S = S.replace(drow, '', 1)
    if not flag: break

print('NO') if S else print('YES')

Solution (Python):
s=input()
S=s[::-1]
i=0
j=0
k=len(S)
while(1):
    if(i==k):
        print("YES")
        break
    if(S[i:i+5]=="maerd" or S[i:i+5]=="esare"):
        i+=5
    if(S[i:i+6]=="resare"):
        i+=6
    if(S[i:i+7]=="remaerd"):
        i+=7
    if(i==j):
        print("NO")
        break
    j=i

Solution (Python):
import sys
s = input()
i = 0
while s[i:i+5] != '':
    if s[i:i+5] == 'dream':
        if s[i+5:i+10] == 'dream':
            i += 5
        elif s[i+5:i+10] == 'erase':
            i += 5
        elif s[i+5:i+7] == 'er':
            i += 7
        else:
            i += 5
    elif s[i:i+5] == 'erase':
        if s[i+5:i+10] == 'dream':
            i += 5
        elif s[i+5:i+10] == 'erase':
            i += 5
        elif s[i+5:i+6] == 'r':
            i += 6
        else:
            i += 5
    else:
        print('NO')
        sys.exit(0)
print('YES')

Solution (Python):
s = input()
q = [0]
flag = False
while len(s) > 0:
    if s[0:5] in ['dream','erase'] and flag == False:
        q.append(s[0:5])
        s = s[5:]
        flag = False
    elif s[0:6] == 'eraser':
        q.append(s[0:6])
        s = s[6:]
        flag = False
    elif s[0:7] == 'dreamer':
        q.append(s[0:7])
        s = s[7:]
        flag = False
    elif q[-1] in ['dream','erase']:
        s = q[-1] + s
        q.pop()
        flag = True
    else:
        print('NO')
        exit()

print('YES')