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 len(S) >= 7 and S[-7:] == 'dreamer':
        S = S[0:-7]
    elif len(S) >= 6 and S[-6:] == 'eraser':
        S = S[0:-6]
    elif len(S) >= 5 and S[-5:] == 'dream':
        S = S[0:-5]
    elif len(S) >= 5 and S[-5:] == 'erase':
        S = S[0:-5]
    else:
        break
if len(S) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
S=input()
X=['dream'[::-1], 'dreamer'[::-1], 'erase'[::-1], 'eraser'[::-1]]
S=S[::-1]
while 1:
    S2=S
    for x in X:
        if x == S[:len(x)]:
            S = S[len(x):]
    if S == '':
        print('YES')
        break
    if S2==S:
        print('NO')
        break

Solution (Python):
s=input()
while len(s):
  for word in ['dream', 'dreamer', 'erase', 'eraser']:
    if (s[-len(word):]==word):
      s=s[:-len(word)]
      break
  else:
    print('NO')
    exit()
print('YES')

Solution (Python):
s = input()
t = ''
add_list = ['dream', 'dreamer', 'erase', 'eraser']
cur = len(s)
for i in range(len(s)):
    if s[-1 - i:cur] in add_list:
        cur = -1 - i
if -cur == len(s):
    print('YES')
else:
    print('NO')

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

Solution (Python):
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    S = S.replace("eraser","")
    S = S.replace("erase","")
    S = S.replace("dreamer","")
    S = S.replace("dream","")
    if S == "":
        print("YES")
    else:
        print("NO")

if __name__ == '__main__':
    main()

Solution (Python):
S = input()

l = ['dream', 'dreamer', 'erase', 'eraser']

while S:
    if len(S) < 5:
        print('NO')
        exit()
    elif S[-5:] in l:
        S = S[:-5]
    elif S[-6:] in l:
        S = S[:-6]
    elif S[-7:] in l:
        S = S[:-7]
    else:
        print('NO')
        exit()
print('YES')

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

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

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

Solution (Python):
s = input()[::-1]
for i in range(10 ** 5):
    if s[:5] == "dream"[::-1] or s[:5] == "erase"[::-1]:
        s = s[5:]
    elif s[:7] == "dreamer"[::-1]:
        s = s[7:]
    elif s[:6] == "eraser"[::-1]:
        s = s[6:]
    else:
        if len(s) > 0:
            print("NO")
            break
        else:
            print("YES")
            break

Solution (Python):
s = input()


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

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

Solution (Python):
S = input()

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

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

Solution (Python):
s=str(input())
s=s[::-1]
t=''

i=0
while i<len(s):
    if s[i:i+5]=='maerd':
        t+='maerd'
        i+=5
    elif s[i:i+5]=='esare':
        t+='esare'
        i+=5
    elif s[i:i+6]=='resare':
        t+='resare'
        i+=6
    elif s[i:i+7]=='remaerd':
        t+='remaerd'
        i+=7
    else:
        break
if s==t:
    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()
sub_s = s.replace("eraser", "")
sub_s = sub_s.replace("erase", "")
sub_s = sub_s.replace("dreamer", "")
sub_s = sub_s.replace("dream", "")
if len(sub_s) == 0:
  print("YES")
else:
  print("NO")

Solution (Python):
s = input()
r_s = s[::-1]
w = ['dream'[::-1], 'dreamer'[::-1], 'erase'[::-1], 'eraser'[::-1]]

num = 0
b = True
while num < len(r_s) and b == True:
    b = False
    for i_w in w:
        if i_w == r_s[num:num+len(i_w)]:
            b = True
            num += len(i_w)

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

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
i = n
while i > 0:
    if s[i-7:i] == 'dreamer' and i-7 >= 0:
        i -= 7
    elif s[i-6:i] == 'eraser' and i-6 >= 0:
        i -= 6
    elif s[i-5:i] == 'dream' and i-5 >= 0:
        i -= 5
    elif s[i-5:i] == 'erase' and i-5 >= 0:
        i -= 5
    else:
        print('NO')
        break
else:
    print('YES')

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

Solution (Python):
S = input()
S = S[::-1]
pattern = ["dream", "dreamer", "erase", "eraser"]
check = True
while check:
    check = False
    for i in range(len(pattern)):
        if S.startswith(pattern[i][::-1]):
            S = S[len(pattern[i]):]
            check = True
if len(S) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
s=input()
while len(s):
  for word in ['dream', 'dreamer', 'erase', 'eraser']:
    if (s[-len(word):]==word):
      s=s[:-len(word)]
      break
  else:
    print('NO')
    sys.exit()
print('YES')

Solution (Python):
S = input()

S = S[::-1]

T = ['dream','dreamer','erase','eraser']
for i in range(4):
    T[i] = T[i][::-1]

i = 0
flag = True
while i<len(S):
    for t in T:
        j = len(t)
        if S[i:i+j] == t:
            i += j
            break
            print(i)
    else:
        flag = False
        break

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

Solution (Python):
s = input()

l = list(s)
l.reverse()
str = "".join(l)

words = [
    "maerd",
    "remaerd",
    "esare",
    "resare"
]

r = "YES"
i = 0
while True:
    if i >= len(str):
        break
    found = False
    for w in words:
        if str[i:i+len(w)] == w:
            found = True
            i += len(w)
            break
    if not found:
        r = "NO"
        break
print(r)

Solution (Python):
import sys
sys.setrecursionlimit(2000000)
s=input()
d='dream'
dr='dreamer'
e='erase'
er='eraser'
l=len(s)
def solve(s,end):
    if end==-1:
        print('YES')
        exit(0)
    if s[end-6:end+1]==dr:
        end-=7
    elif s[end-5:end+1]==er:
        end-=6
    elif s[end-4:end+1]==e or s[end-4:end+1]==d:
        end-=5
    else:
        print('NO')
        exit(0)
    solve(s,end)
solve(s,l-1)

Solution (Python):
import sys
sys.setrecursionlimit(2000000)
s=input()
d='dream'
dr='dreamer'
e='erase'
er='eraser'
l=len(s)
def solve(s,end):
    if end==-1:
        print('YES')
        exit(0)
    if s[end-6:end+1]==dr:
        end-=7
    elif s[end-5:end+1]==er:
        end-=6
    elif s[end-4:end+1]==e or s[end-4:end+1]==d:
        end-=5
    else:
        print('NO')
        exit(0)
    solve(s,end)
solve(s,l-1)

Solution (Python):
S = input()[::-1]
li = ["dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1]]

while len(S) > 0:
  for i in li:
    if S.find(i) == 0:
      S = S[len(i):]
      break
  else:
    print("NO")
    exit()
print("YES")

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()
temp = S

changed = True
while(changed):
  changed = False
  for suffix in ['dream', 'dreamer', 'erase', 'eraser']:
    if temp.endswith(suffix):
      temp = temp[:-len(suffix)]
      changed = True
      break
print('YES' if not temp else 'NO')

Solution (Python):


s = input()

i = len(s)

while i != 0:
    if s[i-5:i] == "dream":
        i -= 5
    elif s[i-7:i] == "dreamer":
        i -= 7
    elif s[i-5:i] == "erase":
        i -= 5
    elif s[i-6:i] == "eraser":
        i -= 6
    else:
        break

if i == 0:
    ans = "YES"
else:
    ans = "NO"

print(ans)

Solution (Python):

from sys import stdin




def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


S = _s()[::-1]

flag = True
while True:
    if S.startswith('res') and S[:6] == 'eraser'[::-1]:
        S = S[6:]
    elif S.startswith('es') and S[:5] == 'erase'[::-1]:
        S = S[5:]
    elif S.startswith('rem') and S[:7] == 'dreamer'[::-1]:
        S = S[7:]
    elif S.startswith('ma') and S[:5] == 'dream'[::-1]:
        S = S[5:]
    else:
        flag = False
        break

    if len(S) == 0:
        break

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    import re
    pattern="^(dream|dreamer|erase|eraser)+$"
    cmp=re.compile(pattern)
    print("YES" if cmp.match(input()) else "NO")
resolve()

Solution (Python):
S = input()[::-1]
ws = ["dream", "dreamer", "erase", "eraser"]
ws = [w[::-1] for w in ws]

s = S
ans = True
T = ""
while S != T:
    if s[:7] == ws[1]:
        T, s = T + ws[1], s[7:]
    elif s[:6] == ws[3]:
        T, s = T + ws[3], s[6:]
    elif s[:5] == ws[2]:
        T, s = T + ws[2], s[5:]
    elif s[:5] == ws[0]:
        T, s = T + ws[0], s[5:]
    else:
        ans = False
        break

print("YES" if ans else "NO")

Solution (Python):
s = input()[::-1]
l = ["maerd", "remaerd", "esare", "resare"]
b = 0
flag = 1
while b!=len(s):
    if s[b:b+5] in ["maerd", "esare"]: b += 5
    elif s[b:b+6] == "resare": b += 6
    elif s[b:b+7] == "remaerd": b += 7
    else: break
else: flag = 0
print("YNEOS"[flag::2])

Solution (Python):
S=input()

while True:
    if len(S)==0:
        print("YES")
        break
    elif len(S)<5:
        print("NO")
        break
    else:
        if S[-5:]=="dream" or S[-5:]=="erase":
            S=S[:-5]
        elif S[-7:]=="dreamer":
            S=S[:-7]
        elif S[-6:]=="eraser":
            S=S[:-6]
        else:
            print("NO")
            break

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

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

S = input()
dp = [False] * (len(S)+1)
dp[0] = True
for i in range(len(S)):
  if not dp[i]:
    continue
  for d in D:
    if S[i:i+len(d)] == d:
      dp[i+len(d)] = True

if dp[len(S)]:
  print('YES')
else:
  print('NO')

Solution (Python):
S = input()

S = S[::-1]
while len(S):
    if S[0] == "r":
        if S[:7] == "remaerd":
            S = S[7:]
        elif S[:6] == "resare":
            S = S[6:]
        else:
            print("NO")
            break
    elif S[0] == "m":
        if S[:5] == "maerd":
            S = S[5:]
        else:
            print("N0")
            break
    elif S[0] == "e":
        if S[:5] == "esare":
            S = S[5:]
        else:
            print("N0")
            break
    else:
        print("NO")
        break
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=str(input())
words = ['eraser','erase','dreamer','dream']
for word in words:
  s=s.replace(word,'')
if len(s) == 0 :
  print('YES')
else :
  print('NO')

Solution (Python):
S=input()
Word = ['eraser','erase','dreamer','dream']
for i in Word:
  S=S.replace(i,'')
if len(S) == 0 :
  print('YES')
else :
  print('NO')

Solution (Python):
S=input()
Word = ['eraser','erase','dreamer','dream']
for i in Word:
  S=S.replace(i,'')
if len(S) == 0 :
  print('YES')
else :
  print('NO')

Solution (Python):
s=input()
idx=len(s)
d={'dream':5,'dreamer':7,'erase':5,'eraser':6}

while True:
	f=True
	for i in d:
		if idx-d[i]>=0 and s[idx-d[i]:idx]==i:
			idx-=d[i]
			f=False
			break
	
	if f:
		print('NO')
		break
	if idx==0:
		print('YES')
		break

Solution (Python):
s = input()
l = ['eraser', 'erase', 'dreamer', 'dream']
for i in l:
    s = s.replace(i, '')
if len(s) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
l = ['eraser', 'dreamer', 'erase', 'dream', 'ase']
for i in l:
    s = s.replace(i, '')
if len(s) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
S = S[::-1]
while len(S) != 0:
  num = len(S)
  if S.find('maerd') == 0:
    S = S.replace('maerd', '', 1)
  if S.find('remaerd') == 0:
    S = S.replace('remaerd', '', 1)
  if S.find('esare') == 0:
    S = S.replace('esare', '', 1)
  if S.find('resare') == 0:
    S = S.replace('resare', '', 1)
  if num == len(S):
    print('NO')
    exit(0)
print('YES')

Solution (Python):
S=input()
l=["eraser","erase","dreamer","dream"]
for i in l:
    S=S.replace(i,"")
if len(S)==0:
    print("YES")
else:
    print("NO")

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

Solution (Python):
s = input()

tc = ['dreamer', 'dream', 'eraser', 'erase']
start = 0
out = True

while out:
  if start >= len(s):
    break
  laststart = start
  for i in tc:
    if s[start:start+len(i)] == i:
      if i == 'dreamer' and s[start+7:start+8] == 'a':
        start += 5
      else:
        start += len(i)
      break
  
  if start == laststart:
    out = False

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

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

Solution (Python):
s = input()[::-1]
a = ['maerd','remaerd','esare','resare']
while s:
    for i in a:
        if i == s[:len(i)]:
            s = s[len(i):]
            break
    else:
        print("NO")
        exit()
print("YES")

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

Solution (Python):
s = list(input())
flag = False
while s:
    if "".join(s[-5:]) == "dream":
        del(s[-5:])
    elif "".join(s[-5:]) == "erase":
        del(s[-5:])
    elif "".join(s[-7:]) == "dreamer":
        del(s[-7:])
    elif "".join(s[-6:]) == "eraser":
        del(s[-6:])
    else:
        flag = False
        break
    
    if len(s) == 0:
       flag = True 
if flag:
   print("YES")
else:
    print("NO")

Solution (Python):
S=input()
W=["dream","dreamer","erase","eraser"]

ans="YES"
while len(S)>0:
  if S.endswith(W[0]):
    S=S[:-len(W[0])]
  elif S.endswith(W[1]):
    S=S[:-len(W[1])]
  elif S.endswith(W[2]):
    S=S[:-len(W[2])]
  elif S.endswith(W[3]):
    S=S[:-len(W[3])]
  else:
    ans="NO"
    break
    
print(ans)

Solution (Python):
s = input()

s_work = s
target_list = ["dream", "dreamer", "erase", "eraser"]

while len(s_work) > 0:
    has_changed = False
    for target in target_list:
        if s_work.endswith(target):
            s_work = s_work[0:-len(target)]
            has_changed = True
            break
    if not has_changed:
        print("NO")
        exit()

print("YES")

Solution (Python):
s=input()[::-1]
for c in("resare","esare","remaerd","maerd"):s=s.replace(c,"")
print("YNEOS"[s!=""::2])

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

s=s.replace('0','')
if s :
	print('NO')
else:
	print('YES')

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

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

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

Solution (Python):
s = input()

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

t = len(s)
hiki = 1
while t > 0 and hiki == 1:
    hiki = 0
    for i in range(len(V)):
        
        if t >= len(V[i]) and s[t-len(V[i]):t] == V[i]:
            t -= len(V[i])
            hiki = 1

if t == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
s=input()[::-1]
n=0
import sys
while n<len(s):
    if s[n:n+5]=="maerd" or s[n:n+5]=="esare":
        n+=5
    elif s[n:n+6]=="resare":
        n+=6
    elif s[n:n+7]=="remaerd":
        n+=7
    else:
        print("NO")
        sys.exit()
print("YES")

Solution (Python):
S = input()

a = ['dream','dreamer','erase','eraser']
for i,ch in enumerate(a):
  a[i] = ch[::-1]

S = S[::-1]

flag = True
while flag:
  if len(S)==0:
    ans  = 'YES'
    flag = False
  elif S[:5] in a:
    S = S[5:]
  elif S[:6] in a:
    S = S[6:]
  elif S[:7] in a:
    S = S[7:]
  else:
    ans  = 'NO'
    flag = False
  
print(ans)

Solution (Python):
s = input()

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

print('YES')

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    t1 = "dream"[::-1]
    t2 = "dreamer"[::-1]
    t3 = "erase"[::-1]
    t4 = "eraser"[::-1]

    s = input().strip()[::-1]

    while True:
        ok = False
        if len(s) >= 7:
            if s[:7] == t2:
                s = s[7:]
                ok = True
        if len(s) >= 6:
            if s[:6] == t4:
                s = s[6:]
                ok = True
        if len(s) >= 5:
            if s[:5] == t1 or s[:5] == t3:
                s = s[5:]
                ok = True
        
        if len(s) < 5 or not ok:
            break
    
    if s != "":
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

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):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$", input()) else "NO")

Solution (Python):
s=input()

ans='YES'
while s:
    if s[-7:]=='dreamer':
        s=s[:-7]
    elif s[-6:]=='eraser':
        s=s[:-6]
    elif s[-5:]=='erase':
        s=s[:-5]
    elif s[-5:]=='dream':
        s=s[:-5]
    else:
        ans='NO'
        break
print(ans)

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

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

Solution (Python):
import time

S = input()[::-1]
keywords = ["dream", "dreamer", "erase", "eraser"]
keywords = [x[::-1] for x in keywords]

start = 0
while True:
    flag = False
    end = None
    for key in keywords:
        key_len = len(key)
        end = start + key_len
        if S[start:end] == key:
            flag = True
            break

    start = end

    if not flag:
        print("NO")
        exit()

    if flag and start == len(S):
        print("YES")
        exit()

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

Solution (Python):
S = input()
L = len(S)

while L:
  f = True
  if L >= 7:
    if S[L-7:L] == "dreamer":
      L -= 7
      f = False
  if L >= 6:
    if S[L-6:L] == "eraser":
      L -= 6
      f = False
  if L >= 5:
    if S[L-5:L] == "dream" or S[L-5:L] == "erase":
      L -= 5
      f = False
  if f:
    break

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

Solution (Python):
S = input()

S1 = S.replace("eraser", "")
S2 = S1.replace("erase", "")
S3 = S2.replace("dreamer", "")
S4 = S3.replace("dream", "")

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

Solution (Python):
s = input()

s = s[::-1]

tmp = 0
for i in range(len(s)):
    
    if tmp == len(s):
        print('YES')
        exit()
    if s[tmp:tmp+6] == 'resare':
        tmp += 6
        continue
    if s[tmp:tmp+5] == 'esare':
        tmp += 5
        continue
    if s[tmp:tmp+5] == 'maerd':
        tmp += 5
        continue
    if s[tmp:tmp+7] == 'remaerd':
        tmp += 7
        continue
    else:
        print('NO')
        exit()

Solution (Python):
s = input()

d = s.count("dream")
dr = s.count("dreamer")
e = s.count("erase")
er = s.count("eraser")
de = s.count("dreamerase")

l = (d + e) * 5 + (dr - de) * 2 + er

print("YES" if len(s) == l else "NO")

Solution (Python):
s = input()

d = s.count("dream")
dr = s.count("dreamer")
e = s.count("erase")
er = s.count("eraser")
de = s.count("dreamerase")
der = s.count("dreameraser")

l = (d + e) * 5 + (dr - de) * 2 + er
print("YES" if l == len(s) else "NO")

Solution (Python):
S=input()
i=len(S)-1
flag=0
T1='dream'
T2='dreamer'
T3='erase'
T4='eraser'

while i>=0:
    if S[i]=='m':
        k=0
        for j in reversed(range(i-4,i+1)):
            
            if S[j]!=T1[4-k]:
                flag=1
            k+=1
        i-=5
    elif S[i]=='e':
        kk=0
        for j in reversed(range(i-4,i+1)):
            
            if S[j]!=T3[4-kk]:
                flag=1
            kk+=1
        i-=5
    elif S[i]=='r' and S[i-1]=='e':
        if S[i-2]=='m':
            kkk=0
            for j in reversed(range(i-6,i+1)):
                
                if S[j] != T2[6-kkk]:
                    flag = 1
                kkk+=1
            i-=7
        elif S[i-2]=='s':
            kkkk=0
            for j in reversed(range(i-5,i+1)):
                
                if S[j] != T4[5-kkkk]:
                    flag = 1
                kkkk+=1
            i-=6
        else:
            i-=1
    else:
        i-=1
if flag==0:
    print('YES')
else:
    print('NO')

Solution (Python):
import re;print("YNEOS"[(re.fullmatch(r"(erase|dream|eraser|dreamer)*",input())==None)::2])

Solution (Python):
def chk(S):
    n = len(S)
    i = n
    while i > 0:
        if S[i-5:i] == 'dream' or S[i-5:i] == 'erase':
            i -= 5
        elif S[i-6:i] == 'eraser':
            i -= 6
        elif S[i-7:i] == 'dreamer':
            i -= 7
        else:
            return False
    return True

S = input()
if chk(S):
    print("YES")
else:
    print("NO")

Solution (Python):
words = ["dream", "dreamer", "erase", "eraser"]
reverse_words = []
for i in words:
  word = list(reversed(i))
  word = "".join(word)
  reverse_words.append(word)


S = input()

while S != "":
  T = S
  length = len(S)
  for j in reverse_words:
    p = (S[:-(len(j)+1):-1])
    if p == j:
      S = (S[:len(T)-len(p)])
  
  if S == T:
    print("NO")
    exit()
print("YES")

Solution (Python):
S=input()

T=''
S=S[::-1]
l,r=0,7
while l<len(S):
  if S[l:r]=='remaerd':
    l=r
    r+=7
  elif S[l:r-1]=='resare':
    l=r-1
    r+=6
  elif S[l:r-2]=='maerd':
    l=r-2
    r+=5
  elif S[l:r-2]=='esare':
    l=r-2
    r+=5
  else:
    print('NO')
    exit()
print('YES')

Solution (Python):
import sys
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()[::-1]
ans = "YES"

while len(s) > 1:
    if s[:5] == "maerd":
        s = s[5:]
    elif s[:7] == "remaerd":
        s = s[7:]
    elif s[:5] == "esare":
        s = s[5:]
    elif s[:6] == "resare":
        s = s[6:]
    else:
        ans = "NO"
        s = ""

print(ans)

Solution (Python):
S=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):
s = input()
p = s.replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')
if len(p) != 0:
  print("NO")
else:
  print("YES")

Solution (Python):

import re
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()
        print('YES' if re.match('^(dream(er)?|eraser?)*$', S) else 'NO')



if __name__ == '__main__':
    AtCoder().main()

Solution (Python):

import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()
        dp = [False] * (len(S) + 1)

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

        dp[0] = True
        for i in range(len(S)):
            if not dp[i]:
                continue
            for word in words:
                if S[i:i + len(word)] == word:
                    dp[i + len(word)] = True

        print('YES' if dp[len(S)] else 'NO')



if __name__ == '__main__':
    AtCoder().main()

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

false_flag = 0
N = len(S)
while N:
    if N >= 7:
        if S[-1] == 'r' and S[-2] == 'e' and S[-3] == 'm' and S[-4] == 'a' and S[-5] == 'e' and S[-6] == 'r' and S[-7] == 'd':
            for i in range(7):
                S.pop()
        elif S[-1] == 'r' and S[-2] == 'e' and S[-3] == 's' and S[-4] == 'a' and S[-5] == 'r' and S[-6] == 'e':
            for i in range(6):
                S.pop()
        elif S[-1] == 'm' and S[-2] == 'a' and S[-3] == 'e' and S[-4] == 'r' and S[-5] == 'd':
            for i in range(5):
                S.pop()
        elif S[-1] == 'e' and S[-2] == 's' and S[-3] == 'a' and S[-4] == 'r' and S[-5] == 'e':
            for i in range(5):
                S.pop()
        else:
            false_flag = 1
            break
    elif N >= 6:
        if S[-1] == 'r' and S[-2] == 'e' and S[-3] == 's' and S[-4] == 'a' and S[-5] == 'r' and S[-6] == 'e':
            for i in range(6):
                S.pop()
        elif S[-1] == 'm' and S[-2] == 'a' and S[-3] == 'e' and S[-4] == 'r' and S[-5] == 'd':
            for i in range(5):
                S.pop()
        elif S[-1] == 'e' and S[-2] == 's' and S[-3] == 'a' and S[-4] == 'r' and S[-5] == 'e':
            for i in range(5):
                S.pop()
        else:
            false_flag = 1
            break
    elif N >= 5:
        if S[-1] == 'm' and S[-2] == 'a' and S[-3] == 'e' and S[-4] == 'r' and S[-5] == 'd':
            for i in range(5):
                S.pop()
        elif S[-1] == 'e' and S[-2] == 's' and S[-3] == 'a' and S[-4] == 'r' and S[-5] == 'e':
            for i in range(5):
                S.pop()
        else:
            false_flag = 1
            break
    else:
        false_flag = 1
        break
    N = len(S)

if false_flag == 1:
    print('NO')
else:
    print('YES')

Solution (Python):
S = input()

words = ['dream','dreamer','erase','eraser']

damm = True

T = ''

while damm:
    damm = False
    for test in words:
        if S.endswith(test+T):
            T = test + T
            damm = True
            break
    if S == T:
        damm = True
        break

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

Solution (Python):

import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()

        while len(S) > 0:
            if S[:11] == 'dreameraser':
                S = S[11:]
            elif S[:10] == 'dreamerase':
                S = S[10:]
            elif S[:7] == 'dreamer':
                S = S[7:]
            elif S[:6] == 'eraser':
                S = S[6:]
            elif S[:5] == 'dream' or S[:5] == 'erase':
                S = S[5:]
            else:
                print('NO')
                exit()
        print('YES')



if __name__ == '__main__':
    AtCoder().main()

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

Solution (Python):
S = input()

for s in ["eraser","erase","dreamer","dream"]:
    S = S.replace(s,"")
    
if S=="":
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
for i in ['eraser','erase','dreamer','dream']:
    s = s.replace(i,'')
if not s:
    print('YES')
else:
    print('NO')

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

s = input()

words = ['dream', 'dreamer', 'erase', 'eraser']
def dfs(idx):
    if idx == len(s):
        return True
    ans = False
    for word in words:
        if s[idx:idx+len(word)] == word:
            ans = ans or dfs(idx + len(word))

    return ans

if dfs(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()
s=s.replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

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

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

for i in word_list:
        S = S.replace(i, '')
if S == '':
    print('YES')
else:
    print('NO')

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

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