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().rstrip()
d = ['dream', 'dreamer']
e = ['erase', 'eraser']
ch = d + e

st = [s]

def search_(s, st, ch):
    if len(s) == 0:
        return
    if len(s) < len(ch):
        return
    else:
        if s[:len(ch)] == ch:
            st.append(s[len(ch):])

def search(s):
    if len(s) == 0:
        return True
    if s[0] == 'd' or s[0] == 'e':
        for ci in ch:
            if search_(s, ci):
                return True
    return False

while st:
    s = st.pop(0)
    if len(s) == 0:
        print('YES')
        exit()
    if s[0] == 'd' or s[0] == 'e':
        for ci in ch:
            search_(s, st, ci)
print('NO')

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

Solution (Python):
S = input()
ok = True
while len(S) > 0:
    if S[0] == 'd':
        if len(S) >= 7 and S[:7] == 'dreamer':
            if len(S) == 7 or S[7] != 'a':
                S = S[7:]
            else:
                S = S[5:]
        elif S[:5] == 'dream':
            S = S[5:]
        else:
            ok = False
    elif S[0] == 'e':
        if len(S) >= 6 and S[:6] == 'eraser':
            if len(S) == 6 or S[6] != 'a':
                S = S[6:]
            else:
                S = S[5:]
        elif S[:5] == 'erase':
            S = S[5:]
        else:
            ok = False
    else:
        ok = False
    if not ok:
        print("NO")
        exit()
print("YES")

Solution (Python):
s = input()
words = ["dreamer", "dream", "eraser", "erase"]
while len(s) > 0:
    judge = [s.endswith(word) for word in words]
    if any(judge):
        s = s[:-len(words[judge.index(True)])]
        if len(s)==0:
            print("YES")
            break
    else:
        print("NO")
        break

Solution (Python):
def c_daydream(S):
    
    s = S[::-1]
    word = ['dream'[::-1], 'dreamer'[::-1], 'erase'[::-1], 'eraser'[::-1]]
    ans = 'YES'
    while s:
        for w in word:
            if s.startswith(w):  
                s = s.replace(w, '', 1)
                break
        else:
            ans = 'NO'
            break
    return ans

S = input().strip()
print(c_daydream(S))

Solution (Python):
words = ["dream", "dreamer", "erase", "eraser"]
s = input()

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

count = 0
while True:
    if s[count:count+5] == words[0]:
        count += 5
    elif s[count:count+7] == words[1]:
        count += 7
    elif s[count:count+5] == words[2]:
        count += 5
    elif s[count:count+6] == words[3]:
        count += 6
    else:
        ans = "NO"
        break
    if count == len(s):
        ans = "YES"
        break
print(ans)

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

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

Solution (Python):
s = input()
n_s = len(s)
S = ''
for i in range(n_s):
    S += s[-i-1]
words = ["maerd", "remaerd", "esare", "resare"]
cnt = 0
result = 'YES'
while True:
    if S[cnt:cnt+5] == 'maerd':
        cnt += 5
    elif S[cnt:cnt+5] == 'esare':
        cnt += 5
    elif S[cnt:cnt+6] == 'resare':
        cnt += 6
    elif S[cnt:cnt+7] == 'remaerd':
        cnt += 7
    else:
        result = 'NO'
        break
    if cnt == n_s:
        break
print(result)

Solution (Python):
S = input()[::-1]
T = ["maerd", "remaerd", "esare", "resare"]

while len(S)>0:
    if S[:5] in T:
        S = S[5:]
    elif S[:6] in T:
        S = S[6:]
    elif S[:7] in T:
        S = S[7:]
    else:
        print("NO")
        exit()

print("YES")

Solution (Python):

s = input()


t = len(s)
flag = True
while t > 0:
    if s[t-5:t] == "erase" or s[t-5:t] == "dream":
        t -= 5
    elif s[t-6:t] == "eraser":
        t -= 6
    elif s[t-7:t] == "dreamer":
        t -= 7
    else:
        flag = False
        print("NO")
        break

if flag:
    print("YES")

Solution (Python):
import re
S = input()
S = re.sub(r'erase(r)?', '',S)
S = re.sub(r'dream(er)?', '', S)

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

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

Solution (Python):
String = input()
t = ['dream', 'dreamer', 'erase', 'eraser']

N=len(String)
index =0 
flag = 'True'
count = 0
while flag=='True':
    count = 0
    for i in t:
        count+=1
        if String.rfind(i)==N-len(i) and String.rfind(i)!=-1:
            String=String[0:N-len(i)]
            N=N-len(i)

            if N==0:
                flag = 'Complete'
            break


        if count==4:
            flag = 'Never'

if flag=='Complete':
    print('YES')
if flag=='Never':
    print('NO')

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

Solution (Python):
s = input()

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

Solution (Python):
s = input()

while len(s) >= 5:

    if s[-5:] == 'dream':
        s = s[:-5]
        continue

    elif s[-7:] == 'dreamer':
        s = s[:-7]
        continue

    elif s[-5:] == 'erase':
        s = s[:-5]
        continue

    elif s[-6:] == 'eraser':
        s = s[:-6]
        continue

    else:
        break

if len(s) > 0:
    print('NO')
else:
    print('YES')

Solution (Python):
S = input()
ans = 0
while len(S)>0:
  if S[:9] == 'dreamerer':
    S = S[7:]
  elif S[:8] == 'dreamerd':
    S = S[7:]
  elif S[:8] == 'dreamera':
    S = S[5:]
  elif S[:6] == 'dreamd':
    S = S[5:]
  elif S[:7] == 'eraserd':
    S = S[6:]
  elif S[:7] == 'erasere':
    S = S[6:]
  elif S[:6] == 'erasee':
    S = S[5:]
  elif S[:6] == 'erased':
    S = S[5:]
  elif S[:] == 'dreamer':
    S = S[7:]
  elif S[:] == 'dream':
    S = S[5:]
  elif S[:] == 'eraser':
    S = S[6:]
  elif S[:] == 'erase':
    S = S[5:]
  else:
    ans += 1
    break
if ans == 0:
  print('YES')
else:
  print('NO')

Solution (Python):
S = input()

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

Solution (Python):
S = input()

candidate_string_list = ['dream', 'dreamer', 'erase', 'eraser']
candidate_string_list_reverse = [elem[::-1] for elem in candidate_string_list]

buffer = ''
for character in S[::-1]:
    buffer += character
    if buffer in candidate_string_list_reverse:
        buffer = ''
        continue

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

Solution (Python):
s = list(str(input()))



for i in range(len(s)- 5):
    if s[i] == 'e' and s[i+1] == 'r' and s[i + 2] == 'a' and s[i + 3] == 's' and s[i + 4] == 'e' and s[i+5] == 'r':
        s[i] = ''
        s[i+1] = ''
        s[i + 2] = ''
        s[i + 3] = ''
        s[i + 4] = ''
        s[i + 5] = ''
        
        
for i in range(len(s)- 4):
    if s[i] == 'e' and s[i+1] == 'r' and s[i + 2] == 'a' and s[i + 3] == 's' and s[i + 4] == 'e' :
        s[i] = ''
        s[i+1] = ''
        s[i + 2] = ''
        s[i + 3] = ''
        s[i + 4] = ''
        

    
    
    
for i in range(len(s)- 6):
    if s[i] == 'd' and s[i+1] == 'r' and s[i + 2] == 'e' and s[i + 3] == 'a' and s[i + 4] == 'm' and s[i + 5] == 'e' and s[i + 6] == 'r':
        s[i] = ''
        s[i+1] = ''
        s[i + 2] = ''
        s[i + 3] = ''
        s[i + 4] = ''
        s[i + 5] = ''
        s[i+6] = ''
        

    
for i in range(len(s)- 4):
    if s[i] == 'd' and s[i+1] == 'r' and s[i + 2] == 'e' and s[i + 3] == 'a' and s[i + 4] == 'm' :
        s[i] = ''
        s[i+1] = ''
        s[i + 2] = ''
        s[i + 3] = ''
        s[i + 4] = ''

        
while '' in s:
    s.remove('')
    
if s == []:
    print('YES')
else:
    print('NO')

Solution (Python):




S = input()
T_s = ["dream", "dreamer", "erase", "eraser"]

while len(S):
    for T in T_s:
        if T == S[-len(T):]:
            S = S[:-len(T)]
            break
    else:
        print("NO")
        break
else:
    print("YES")

Solution (Python):
a = input()
a = a[::-1]
b = ['maerd','remaerd','esare','resare']
flag = 0
while flag >= 0:
    flag = 0
    if a[:5] == b[0]:
        a = a[5:]
    elif a[:5]==b[2]:
        a = a[5:]
    elif a[:7] == b[1]:
        a = a[7:]
    elif a[:6] == b[3]:
        a = a[6:]
    elif a == '':
        print('YES')
        break
    else:
        flag = -1
        print('NO')

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

Solution (Python):
S = input()
N = len(S)
R = S[::-1]
dic = ['maerd','remaerd','esare','resare']
i = 0
while i < N:
    for w in dic:
        if R[i:].startswith(w):
            i += len(w)
            break
    else:
        print('NO')
        exit()
print('YES')

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

i = 0
ans = True
while True:
    if i >= n:
        break
    if s[i:i+7] == "remaerd":
        i += 7
    elif s[i:i+6] == "resare":
        i += 6
  
    elif s[i:i+5] == "maerd" or s[i:i+5] == "esare":
        i += 5
    else:
        ans = False
        break

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

Solution (Python):
divide = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(4):
    divide[i] = divide[i][::-1]
s = input()[::-1]
can = True
i = 0
while i < len(s):
    can2 = False
    for j in range(4):
        d = divide[j]
        if s[i:i+len(d)] == d:
            can2 = True
            i += len(d)
    if not can2:
        can = False
        break

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

Solution (Python):
N = input()

if N.replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '') == '':
    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","")

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

Solution (Python):
import sys
S = input()
S = S[::-1]
substring_list = ['maerd', 'remaerd', 'esare', 'resare']
i = 0
while i < len(S):
    ok = False
    for substring in substring_list:
        if S[i:i+len(substring)] == substring:
            i += len(substring)
            ok = True
            break
    if not ok:
        print('NO')
        sys.exit()
print('YES')

Solution (Python):
s = input()
judge = True
while 0<len(s): 
    if s[:5]=='dream':
        s = s[5:]
        if s[:2]=='er' and not s[:5]=='erase':
            s = s[2:]
    elif s[:5]=='erase':
        s = s[5:]
        if s[:1]=='r':
            s = s[1:]
    else:
        judge = False
        break
print('YES' if judge else 'NO')

Solution (Python):
s = input()[::-1]
ls = [word[::-1] for word in ["dream", "dreamer", "erase", "eraser"]]
i = 0
while s:
    word = ls[i]
    if s.find(word) == 0:
        s = s[len(word):]
        i = 0
    else:
        i += 1
        if i > 3:
            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 = input()
while s:
    for p in ('dreamer', 'eraser', 'dream', 'erase'):
        if s.endswith(p):
            s = s[:-len(p)]
            break
    else:
        break
print('YES' if s=='' else 'NO')

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

Solution (Python):
string = input()
erase = 'erase'
eraser = 'eraser'
dream = 'dream'
dreamer = 'dreamer'
er = 'er'
a = 'a'
r = 'r'
while True:
    if string[:5] == dream:
        if string[5:7] == er:
            if string[7:8] == a:
                string = string[5:]
            else:
                string = string[7:]
        else:
            string = string[5:]
    elif string[:5] == erase:
        if string[5:6] == r:
            if string[6:7] == a:
                string = string[5:]
            else:
                string = string[6:]
        else:
            string = string[5:]
    elif string == '':
        print('YES')
        break
    else:
        print('NO')
        break

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

solve()

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

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

words = [w[::-1] for w in ["dream", "dreamer", "erase", "eraser"]]
while True:
    removed = False
    for w in words:
        if S.startswith(w):
            S = S[len(w):]
            removed = True
            break
    if S == "":
        print("YES")
        break
    if not removed:
        print("NO")
        break

Solution (Python):
dp = [0] * 100005

S = raw_input()

dp[0] = 1

for i in range(len(S)):
    if dp[i] == 0:
        continue
    if i + 5 <= len(S) and S[i:i+5] == "dream":
        dp[i + 5] = 1
    if i + 7 <= len(S) and S[i:i+7] == "dreamer":
        dp[i + 7] = 1
    if i + 5 <= len(S) and S[i:i+5] == "erase":
        dp[i + 5] = 1
    if i + 6 <= len(S) and S[i:i+6] == "eraser":
        dp[i + 6] = 1

print "YES" if (dp[len(S)] == 1) else "NO"

Solution (Python):
def c_daydream(S):
    
    s = S[::-1]
    n = len(s)
    while True:
        if n == 0:
            return 'YES'
        elif n >= 5 and s[:5] == 'maerd':
            s = s[5:]
        elif n >= 7 and s[:7] == 'remaerd':
            s = s[7:]
        elif n >= 5 and s[:5] == 'esare':
            s = s[5:]
        elif n >= 6 and s[:6] == 'resare':
            s = s[6:]
        else:
            return 'NO'
        n = len(s)
    return None

S = input().strip()
print(c_daydream(S))

Solution (Python):


s = input()
s = s[::-1]
flag = 0
while(flag >= 0):
    flag = 0
    if(s[:5] == 'maerd'):
        s = s[5:]
    elif(s[:5] == 'esare'):
        s = s[5:]
    elif(s[:6] == 'resare'):
        s = s[6:]
    elif(s[:7] == 'remaerd'):
        s = s[7:]
    elif(s == ''):
        print('YES')
        break
    else:
        flag = -1
        print('NO')

Solution (Python):
inline = list(input())
inline.reverse()

a1 = list("dream")
a2 = list("erase")
a3 = list("dreamer")
a4 = list("eraser")
a1.reverse()
a2.reverse()
a3.reverse()
a4.reverse()
i = 0
key = 0
while i < len(inline):
    if inline[i] == "m":
        if inline[i+1] == a1[1] and inline[i+2] == a1[2] and inline[i+3] == a1[3] and inline[i+4] == a1[4]:
            i+=5
        else:
            key += 1
            break
    elif inline[i] == "r" and inline[i+1] == "e":
        if inline[i+2] == a3[2] and inline[i+3] == a3[3] and inline[i+4] == a3[4] and inline[i+5] == a3[5] and inline[i+6] == a3[6]:
            i+=7
        elif inline[i+2] == a4[2] and inline[i+3] == a4[3] and inline[i+4] == a4[4] and inline[i+5] == a4[5]:
            i+= 6
        else :
            key += 1
            break

    elif inline[i] == "e":
        if inline[i+1] == a2[1] and inline[i+2] == a2[2] and inline[i+3] == a2[3] and inline[i+4] == a2[4]:
            i+=5
        else:
            key += 1
    else:
        key += 1
        break


if key >= 1:
    print("NO")
else:
    print("YES")

Solution (Python):
s=input()
w=["dream","dreamer","erase","eraser",""]
while True:
    

    if s[0:5]==w[0]:
        if s[5:7]=="er": 
            if s[7]=="a": 
                s=s[5:]
            else:
                s=s[7:]
        else:
            s=s[5:]

    elif s[0:5]==w[2]:
        if s[5]=="r": 
            s=s[6:]
        else:
            s=s[5:]
    else:
        print("NO")
        exit()

    if len(s)<=7:
        if s in w:
            print("YES")
            exit()
        else:
            print("NO")
            exit()

Solution (Python):
s = input()
l = ['dream', 'dreamer', 'erase', 'eraser']
pos = 0
flg = True
can = False
while flg:
    flg = False
    for i in range(len(l)):
        st = len(l[i])+pos
        if st<=len(s):
            if s[st*(-1):len(s)-pos] == l[i]:
                pos += len(l[i])
                if pos == len(s):
                    can = True
                    break
                flg = True
if can == True:
    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()

p = len(s)

while p > 0:
    if p >= 7 and s[(p - 7):p] == "dreamer":
        p -= 7
    elif p >= 6 and s[(p - 6):p] == "eraser":
        p -= 6
    elif p >= 5 and s[(p - 5):p] == "dream":
        p -= 5
    elif p >= 5 and s[(p - 5):p] == "erase":
        p -= 5
    else:
        p = -1
        break

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

Solution (Python):
s = input()
s = s[::-1]
while len(s)>4:
	if s[:5]=="maerd" or s[:5]=="esare": s=s[5:]
	elif s[:6]=="resare": s=s[6:]
	elif s[:7]=="remaerd": s=s[7:]
	else: break
if s=="": print("YES")
else: print("NO")

Solution (Python):


import sys

s = input()

a1 = "dream"
a2 = "dreamer"
a3 = "erase"
a4 = "eraser"

while True:
    if len(s) < 5:
        print("NO")
        sys.exit(0)
    elif len(s) == 5:
        if s in ["dream", "erase"]:
            print("YES")
        else:
            print("NO")
        sys.exit(0)
    elif len(s) == 6:
        if s == "eraser":
            print("YES")
        else:
            print("NO")
        sys.exit(0)
    elif len(s) == 7:
        if s == "dreamer":
            print("YES")
        else:
            print("NO")
        sys.exit(0)
    else:
        if s[:8] in ["dreamerd", "dreamere"]:
            s = s[7:]
        elif s[:6] in ["dreamd", "dreame", "erasee", "erased"]:
            s = s[5:]
        elif s[:7] in ["eraserd", "erasere"]:
            s = s[6:]
        else:
            print("NO")
            sys.exit(0)

Solution (Python):
s = input()
key = 1
i = 0

while i < len(s):
    if s[i:i+5] == "dream":
        if s[i+5:i+10] != "erase" and s[i+5:i+7] == "er":
            i += 7
        elif s[i+5:i+11] == "eraser":
            i += 11
        elif s[i+5:i+10] == "erase":
            i += 10
        else:
            i += 5
    elif s[i:i+6] == "eraser":
        i += 6
    elif s[i:i+5] == "erase":
        i += 5
    else:
        key = 0
        break

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

Solution (Python):
given = ["dreamer", "dream", "eraser", "erase"]
S = input()
while len(S) > 0:
    judge = [S.endswith(i) for i in given]
    if any(judge):
        target = given[judge.index(True)]
        S = S[:-len(target)]
        if len(S) == 0:
            print("YES")
            break
    else:
        print("NO")
        break

Solution (Python):
given = ["dreamer", "dream", "eraser", "erase"]
S = input()
while len(S) > 0:
    tmp = S
    for i in given:
        if S.endswith(i):
            S = S[::-1].replace(i[::-1], "", 1)[::-1]
            if len(S) == 0:
                print("YES")
                break
    if tmp == S:
        print("NO")
        break

Solution (Python):
given = [i[::-1] for i in ["dreamer", "eraser", "dream", "erase"]]
S = input()[::-1]
while S != "":
    tmp = S
    for i in given:
        if S.startswith(i):
            S = S.replace(i, "", 1)
            if len(S) == 0:
                print("YES")
                break
    if tmp == S:
        print("NO")
        break

Solution (Python):
s = input()
index = 0
result = 'YES'
while index < len(s):
    if s.startswith('dreameraser', index):
        index += 11
    elif s.startswith('dreamerase', index):
        index += 10
    elif s.startswith('dreamer', index):
        index += 7
    elif s.startswith('eraser', index):
        index += 6
    elif s.startswith('dream', index) or s.startswith('erase', index):
        index += 5
    else:
        result = 'NO'
        break
print(result)

Solution (Python):






def initialize():
    count = 1
    extracted_s = ''
    return count, extracted_s


if __name__ == '__main__':
    s = input()
    words = ['dream', 'dreamer', 'erase', 'eraser']
    count, extracted_s = initialize()

    while len(s) != len(extracted_s):
        extracted_s = s[len(s) - count:]
        count += 1

        if extracted_s in words:
            s = s[:-len(extracted_s)]
            count, extracted_s = initialize()

    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()
i = 0
while True:
	if i == len(s):
		print('YES')
		break
	if len(s) - i < 5:
		print('NO')
		break
	if s[i] == 'd':
		if s[i:i+5] != 'dream':
			print('NO')
			break
		if len(s) - i < 7:
			i += 5
			continue
		if s[i:i+7] != 'dreamer':
			i += 5
			continue
		if len(s) - i == 7 or s[i+7] != 'a':
			i += 7
			continue
		i += 5
		continue
	if s[i] == 'e':
		if s[i:i+5] != 'erase':
			print('NO')
			break
		if len(s) - i < 6:
			i += 5
			continue
		if s[i:i+6] != 'eraser':
			i += 5
			continue
		if len(s) - i == 6 or s[i+6] != 'a':
			i += 6
			continue
		i += 5
		continue
	print('NO')
	break

Solution (Python):
import re

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

S = input()
Sr = ''
for i in range(len(S)):
    Sr += S[-(i+1)]

temp = ''
while Sr != '':
    if temp == Sr:
        print('NO')
        break
    temp = Sr
    
    for word in wordlist:
        if re.match(word, Sr):
            Sr = Sr[len(word):]
        
else:
    print('YES')

Solution (Python):
def main():
    S = input()

    s = len(S)
    while s > 4:
        e = s
        for i in (5, 6, 7):
            s = e - i
            if S[s:e] in ('dream', 'dreamer', 'erase', 'eraser'):
                break
        else:
            print('NO')
            return
    print('YES')

main()

Solution (Python):
s = input()
revs = s[::-1] + "Z"
i = 0

while(True):
 if revs[i:i+5]=="maerd": i+=5
 elif revs[i:i+7]=="remaerd": i+=7
 elif revs[i:i+5]=="esare": i+=5
 elif revs[i:i+6]=="resare": i+=6
 elif revs[i]=="Z":
  print("YES")
  break
 else:
  print("NO")
  break

Solution (Python):
s = input() + "ZZZZZZZZZZZZZ"

i = 0
ans = "NO"

while(True):

 if s[i:i+5]=="dream":
  if s[i+5]=="d" or s[i+5]=="Z" or s[i+5:i+8]=="era":
   i += 5
  elif s[i+5:i+8]=="erd" or s[i+5:i+8]=="ere" or s[i+5:i+8]=="erZ":
   i += 7
  else:
   break
 
 elif s[i:i+5]=="erase":
  if s[i+5]=="d" or s[i+5]=="e" or s[i+5]=="Z":
   i += 5
  elif s[i+5]=="r":
   i += 6
  else:
   break

 elif s[i]=="Z":
  ans = "YES"
  break

 else:
  break

print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

s = ns()
s = list(s)
flag = 1
while len(s) != 0:
    if s[-5:] == list("dream") or s[-5:] == list("erase"):
        del s[-5:]
        continue
    elif s[-6:] == list("eraser"):
        del s[-6:]
        continue
    elif s[-7:] == list("dreamer"):
        del s[-7:]
        continue
    else:
        flag = 0
        break

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

Solution (Python):
s = input()[::-1]
l = ["dream", "dreamer", "erase", "eraser"]
start = 0

for i in range(len(l)):
  l[i] = l[i][::-1]
  
def f():
  i = 0
  j = 0
  while j <= len(s):
    can = False
    for d in l:
      if s[i:i+len(d)] == d:
        i += len(d)
        can = True

    if i == len(s) and can == True:
      return 'YES'

    if can == False:
      return 'NO'
    j += 1

  return 'YES'

print(f())

Solution (Python):
S = input()
word5 = ['dream','erase']
word6 = ['dreamer',]

i = len(S)

while True:
	if i - 5 < 0:
		break

	if S[i-5:i] in word5:
		i -= 5

	elif i - 6 >= 0 and S[i-6:i] == 'eraser':
		i -= 6

	elif i - 7 >= 0 and S[i-7:i] == 'dreamer':
		i -= 7

	else:
		break

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

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

Solution (Python):
import itertools
S = input()
L = ["dreamer", "eraser", "erase", "dream"]
R = list(itertools.permutations(L))
flag=0
for sg in R:
    T=S
    for i in range(4):
        T=T.replace(sg[i], "")
    if T=="":
        flag=1
        break
if flag==1:
    print("YES")
else:
    print("NO")

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

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

Solution (Python):
s = input()[::-1]
gogun = ["maerd","remaerd","esare","resare"]
while (len(s)>0):
    if len(s) >= 5 and (s[:5] == gogun[0] or s[:5] == gogun[2]):
        s = s[5:]
    elif len(s) >= 6 and s[:6] == gogun[3]:
        s = s[6:]
    elif len(s) >= 7 and s[:7] == gogun[1]:
        s = s[7:]
    else:
        print("NO")
        break
else:
    print("YES")

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

    i = 0
    f = True
    while i < len(s):
        if s[i:i+5] == "maerd":
            i += 5
        elif s[i:i+5] == "esare":
            i += 5
        elif s[i:i+6] == "resare":
            i += 6
        elif s[i:i+7] == "remaerd":
            i += 7
        else:
            f = False
            break

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


    

if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
S=input()

t=len(S)
for i in range(len(S)//4+1):
    if S[t-7:t]=="dreamer":
        t-=7
    elif S[t-6:t]=="eraser":
        t-=6
    elif S[t-5:t]=="dream" or S[t-5:t]=="erase":
        t-=5
    else:
        break

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

Solution (Python):
S=input()

t=len(S)
for i in range(len(S)//4+1):
    if S[t-7:t]=="dreamer":
        t-=7
    elif S[t-6:t]=="eraser":
        t-=6
    elif S[t-5:t]=="dream" or S[t-5:t]=="erase":
        t-=5
    else:
        break

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

Solution (Python):
def c_Daydream(S):
    
    s = S[::-1]
    while True:
        if len(s) >= 5 and s[0:5] == 'maerd':
            s = s[5:]
        elif len(s) >= 7 and s[0:7] == 'remaerd':
            s = s[7:]
        elif len(s) >= 5 and s[0:5] == 'esare':
            s = s[5:]
        elif len(s) >= 6 and s[0:6] == 'resare':
            s = s[6:]
        elif len(s) == 0:
            return 'YES'
        else:
            return 'NO'
S = input().strip()
print(c_Daydream(S))

Solution (Python):
S = input()

while True:
    if "dreameraser" in S:
        S = S.replace("dreameraser", "")
    elif "dreamerase" in S:
        S = S.replace("dreamerase", "")
    elif "dreamer" in S:
        S = S.replace("dreamer", "")
    elif "eraser" in S:
        S = S.replace("eraser", "")
    elif "dream" in S:
        S = S.replace("dream", "")
    elif "erase" in S:
        S = S.replace("erase", "")
    else:
        if S == "":
            print("YES")
        else:
            print("NO")
        break