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):

import sys

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



def main():
    global PARTS2
    sys.setrecursionlimit(100000)
    S = input()

    
    
    

    

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


def judgeStr2(target: str) -> bool:
    currentStr = target
    while currentStr != "":
        isNextOK = False
        for item in PARTS:
            if not currentStr.startswith(item):
                continue

            tempStr = currentStr.replace(item, "", 1)
            if tempStr == "":
                return True

            for item2 in PARTS:
                if tempStr.startswith(item2):
                    isNextOK = True
                    break

            if isNextOK:
                currentStr = tempStr
                break
        if isNextOK:
            continue
        else:
            break
    return currentStr == ""


def judgeStr(target: str) -> bool:
    for item in PARTS:
        if target.startswith(item):
            tempStr = target.replace(item, "", 1)
            if tempStr == "" or judgeStr(tempStr):
                return True

    return False


if __name__ == "__main__":
    main()

Solution (Python):
S = input()

while len(S) > 0:
    if S[-7:] == 'dreamer':
        S = S[:-7]
    elif S[-6:] == 'eraser':
        S = S[:-6]
    elif S[-5:] == 'dream':
        S = S[:-5]
    elif S[-5:] == 'erase':
        S = S[:-5]
    else :
        break

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

Solution (Python):

import math



ans = 0
s = input()
n = len(s)
s=s[::-1]
i=0
flg=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:
        flg=False
        break
if flg:
    print("YES")
else:
    print("NO")

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

s = "".join(list(reversed(s)))

y = 0

for _ in range(100000):
    if y == len(s):
        print("YES")
        break
    elif s[y:y+7] == "remaerd":
        y += 7
        
    elif s[y:y+6] == "resare":
        y += 6
        
    elif s[y:y+5] == "esare":
        y += 5
        
    elif s[y:y+5] == "maerd":
        y += 5
    else:
        print("NO")
        break

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

s = "".join(reversed(list(s)))
while n:
    if s.startswith("maerd"):
        n -= 5
        s = s[5:]
    elif s.startswith("remaerd"):
        n -= 7
        s = s[7:]
    elif s.startswith("esare"):
        n -= 5
        s = s[5:]
    elif s.startswith("resare"):
        n -= 6
        s = s[6:]
    else:
        break
print("YES" if n == 0 else "NO")

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

Solution (Python):

def main():
    s = list(input())
    
    while len(s) != 0:
        if ''.join(s[-5:]) == 'dream':
            for _ in range(5):
                s.pop()
        elif ''.join(s[-7:]) == 'dreamer':
            for _ in range(7):
                s.pop()
        elif ''.join(s[-5:]) == 'erase':
            for _ in range(5):
                s.pop()
        elif ''.join(s[-6:]) == 'eraser':
            for _ in range(6):
                s.pop()
        else:
            print('NO')
            exit()
    else:
        print('YES')


if __name__ ==  "__main__":
    main()

Solution (Python):








s = input()

ans = "YES"
while True:
    if len(s) < 5:
        ans = "NO"
        break
    elif len(s) == 5:
        if (s != "dream" and s != "erase"):
            ans = "NO"
        break
    elif len(s) == 6:
        if (s != "eraser"):
            ans ="NO"
        break
    elif len(s) == 7:
        if (s != "dreamer"):
            ans ="NO"
        break
    else:
        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:
            ans = "NO"
            break
print(ans)

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

	border = len(S)

	while True:
		if S[border - 5: border] == "dream":
			border -= 5
		elif S[border - 7: border] == "dreamer":
			border -= 7
		elif S[border - 5: border] == "erase":
			border -= 5
		elif S[border - 6: border] == "eraser":
			border -= 6
		else:
			break

	if border == 0:
		print("YES")
	else:
		print("NO")
 
 
if __name__ == "__main__":
  	main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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



if __name__ == '__main__':
    main()

Solution (Python):
import sys
s = list(input())
while(len(s) > 0):
    if (''.join(s[-5:]) == 'dream'):
        for i in range(5):
            s.pop()
    elif (''.join(s[-5:]) == 'erase'):
        for i in range(5):
            s.pop()
    elif (''.join(s[-6:]) == 'eraser'):
        for i in range(6):
            s.pop()
    elif (''.join(s[-7:]) == 'dreamer'):
        for i in range(7):
            s.pop()
    else:
        print('NO')
        sys.exit()
print('YES')

Solution (Python):
S=input()
W=[0]*(len(S)+1)
W[0]=1
words=['dream','dreamer','erase','eraser' ]
for i in range(len(S)):
  if W[i]==0:
    continue
  for k in words:
    if S[i:i+len(k)]==k:
      W[i+len(k)]=1
if W[len(S)]==1:
  print("YES")
  exit()
else:
  print("NO")

Solution (Python):
s = ''.join(list(reversed(input())))
words = {'remaerd', 'resare', 'maerd', 'esare'}
word = ''
j = 0
for i in range(len(s)-4):
    if j == 0:
        a = s[i:i+5]
        b = s[i:i+6]
        c = s[i:i+7]
        if a in words:
            word += a
            j = 4
        elif b in words:
            word += b
            j = 5
        elif c in words:
            word += c
            j = 6
    else:
        j -= 1
if word == s:
    print('YES')
else:
    print('NO')

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

Solution (Python):
import sys
s=''.join(reversed(list(input())))
t=''
text = ['maerd', 'esare', 'resare', 'remaerd']
while len(t) < len(s):
  for tx in text:
    if t + tx == s[:len(t+tx)]:
      t += tx
      break
  else:
    print('NO')
    sys.exit()
print('YES')

Solution (Python):
def main():
    text = input()
    words = ['dreamer', 'eraser', 'dream', 'erase']
    length = len(text)
    while text:
        for word in words:
            if text.endswith(word):
                text = text[:-len(word)]
        else:
            if (new := len(text)) != length:
                length = new
            else:
                return 'NO'
    return 'YES'


if __name__ == '__main__':
    ans = main()
    print(ans)

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

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

ind = 0
while ind < len(S):
    flg = False
    for s in Ss:
        n = len(s)
        if S_r[ind:ind+n] == s:
            ind += n
            flg = True
    if not flg:
        print("NO")
        exit()
print("YES")

Solution (Python):
s = input()
should_continue = True

while should_continue:
    should_continue = False
    if len(s) >= 6:
        s6 = s[0:6]
        if s6 == "dreamd" or s6 == "erased" or s6 == "erasee":
            s = s[5:]
            should_continue = True
    if len(s) >= 7:
        if s6 == "eraser":
            s = s[6:]
            should_continue = True
    if len(s) >= 8:
        s8 = s[:8]
        if s8 == "dreamerd" or s8 == "dreamere":
            s = s[7:]
            should_continue = True
        elif s8 == "dreamera":
            s = s[5:]
            should_continue = True

if s == "dream" or s == "dreamer" or s == "erase" or s == "eraser":
    print("YES")
else:
    print("NO")

Solution (Python):
s=input()
x=["eraser","erase","dreamer","dream",]
t=[]
flag=0
for i in range(4):
    if x[i] in s:
        s=s.replace(x[i],"1")
S=len(s)

for j in range(S) :
    if s[j]!="1":
        print("NO")
        flag+=1
        break

if flag==0:
    print("YES")

Solution (Python):
S = input()
s = S.split("a")
H = S.split("a",1)
h=H[0]
B = S.rsplit("a",1)
b=B[1]
X = set(s)
if h in ["dre","er"]:
  if b in ["m","mer","se","ser"]:
    if X <= {"dre","er","m","mer","se","ser","mdre","merdre","mer","merer","sedre","serdre","seer","serer"}:
      print("YES")
    else:
      print("NO")  
  else:
    print("NO")
else:
  print("NO")

Solution (Python):
s = input()
now = len(s)
while 1:
    flag = 1
    if 7 <= now:
        if s[now-7:now] == "dreamer":
            now -= 7
            flag = 0
    if 6 <= now:
        if s[now-6:now] == "eraser":
            now -= 6
            flag = 0
    if 5 <= now:    
        if s[now-5:now] == "erase":
            now -= 5
            flag = 0
        if s[now-5:now] == "dream":
            now -= 5
            flag = 0
    if flag:
        print("NO")
        exit()
    if now == 0:
        print("YES")
        exit()
    elif now < 5:
        print("NO")
        exit()

Solution (Python):
S=input()

dp=[False]* (len(S)+1)

dp[0] = True

for i in range(1, len(dp)):
  for w in [ 'dream', 'dreamer', 'erase', 'eraser']:
    if dp[i-len(w)] and w == S[i-len(w):i]:
      dp[i] = True

if dp[-1]:
  print('YES')
else:
  print('NO')

Solution (Python):
import re;print("YNEOS"[re.match("(erase(r)?|dream(er)?)+$",input())is None::2])

Solution (Python):
import re
print("YNEOS"[re.fullmatch(r"(erase(r)?|dream(er)?)+", input()) is None::2])

Solution (Python):

import re
s = input()
s = re.sub(r'erase(r|)',"", s)
s = re.sub(r'dream(er|)',"", s)
print('YNEOS'[s!=""::2])

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):
S =input()
ls_S = list(S)
ans = "YES"

while len(ls_S) != 0:
  if "".join(ls_S[len(ls_S)-5:]) == "dream":
    ls_S = ls_S[:len(ls_S)-5]
  elif "".join(ls_S[len(ls_S)-5:]) == "erase":
    ls_S = ls_S[:len(ls_S)-5]
  elif "".join(ls_S[len(ls_S)-7:]) == "dreamer":
    ls_S = ls_S[:len(ls_S)-7] 
  elif "".join(ls_S[len(ls_S)-6:]) == "eraser":
    ls_S = ls_S[:len(ls_S)-6]
  else:
    ans = "NO"
    break
print(ans)

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

B=""
while B!=S:
    B=S
    for X in F:
        a=len(X)
        if S[-a:]==X:
            S=S[:-a]

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

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

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

pointer=0
while 1:
  if pointer == len(S):
    print('YES')
    exit()
  if pointer >= len(S)-4:
    print('NO')
    exit()
  if S[pointer:pointer+5] in {'dream'[::-1],'erase'[::-1]}:
    pointer += 5
    continue
  elif S[pointer:pointer+6] == 'eraser'[::-1]:
    pointer += 6
    continue
  elif S[pointer:pointer+7] == 'dreamer'[::-1]:
    pointer += 7
    continue
  else:
    print('NO')
    exit()

Solution (Python):
S = input()

idx = len(S)
while idx > 0:
    if S[idx-7:idx] == "dreamer":
        idx -= 7
    elif S[idx-6:idx] == "eraser":
        idx -= 6
    elif S[idx-5:idx] in ["dream", "erase"]:
        idx -= 5
    else:
        print("NO")
        break
else:
    print("YES")

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

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

Solution (Python):
s=str(input()[::-1])
word = ["maerd","esare","resare","remaerd"]
ans="YES"
i=0
while True:
    if i==len(s):
        break
    if s[i:i+5]==word[0]:
        i+=5
    elif s[i:i+5]==word[1]:
        i+=5
    elif s[i:i+6]==word[2]:
        i+=6
    elif s[i:i+7]==word[3]:
        i+=7
    else:
        ans="NO"
        break
print(ans)

Solution (Python):
s = input()

while len(s) >= 5:
    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

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

Solution (Python):
T=input()
while True:
  if T==str():
    print("YES")
    exit()
  if len(T)<5:
    print("NO")
    exit()
  if T[-5:]=="dream":
    T=str(T[:-5])
  elif T[-7:]=="dreamer":
    T=str(T[:-7])
  elif T[-5:]=="erase":
    T=str(T[:-5])
  elif T[-6:]=="eraser" :
    T=str(T[:-6])
  else:
    print("NO")
    exit()

Solution (Python):
s = input()

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

def checklast(s):
    
    for word in str_list:
        if s[-len(word):] == word:
            return s[:-len(word)]
    else:
            return None
    
for i in range(100000):
    if s is None:
        print("NO")
        break
    if len(s) == 0:
        print("YES")
        break
    s = checklast(s)

Solution (Python):
S = input()
T = ''

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

for word in add_word:
    S = S.replace(word, '')

if S == T:
    ans = 'YES'
else:
    ans = 'NO'

print(ans)

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)







INF = 1 << 50

def dfs(n,seq):
    if seq[n:] in ['dreamer', 'dream', 'erase', 'eraser']:
        return True

    if seq[n:n+7] == 'dreamer':
        if dfs(n+5, seq):
            return True
        elif dfs(n+7, seq):
            return True
        else:
            return False

    elif seq[n:n+5] == 'dream':
        return dfs(n+5, seq)

    elif seq[n:n+6] == 'eraser':
        return dfs(n+6, seq)

    elif seq[n:n+5] == 'erase':
        return dfs(n+5, seq)
    else:
        return False


def run():
    S = input()
    if dfs(0,S):
        print('YES')
    else:
        print('NO')


if __name__ == "__main__":
    run()

Solution (Python):
def resolve():
    S = input()
    pats = ('dream', 'dreamer', 'erase', 'eraser')

    dp = [False for _ in range(len(S) + 1)]
    dp[0] = True
    for i in range(len(S)):
        if not dp[i]:
            continue
        for pat in pats:
            if S[i:i + len(pat)] == pat:
                dp[i + len(pat)] = True
    if dp[-1]:
        print("YES")
    else:
        print("NO")


resolve()

Solution (Python):
s=input()
t=''
while s!='':
  if len(s)<=4:
    break
  elif s[:5]=='dream':
    if len(s)>=10 and s[5:10]=='erase':
      s=s[5:]
      continue
    elif len(s)>=7 and s[5:7]=='er':
      s=s[7:]
      continue
    else:
      s=s[5:]
      continue
  elif s[:5]=='erase':
    if len(s)>=6 and s[5]=='r':
      s=s[6:]
      continue
    else:
      s=s[5:]
      continue
  
  elif s[:5]!='dream' and s[:5]!='erase':
    break
      
    
if s=='':
  print('YES')
  
else:
  print('NO')

Solution (Python):
S = input()

idx = len(S)
while True:
    if S[idx-5:idx] == 'dream':
        idx -= 5
    elif S[idx-6:idx] == 'eraser':
        idx -= 6
    elif S[idx-5:idx] == 'erase':
        idx -= 5
    elif S[idx-7:idx] == 'dreamer':
        idx -= 7
    else:
        print('NO')
        break

    if idx == 0:
        print('YES')
        break

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

Solution (Python):






S = input()





f = True
s_reversed = S[::-1]
words = ['maerd', 'remaerd', 'esare', 'resare']
while s_reversed:
    length = len(s_reversed)
    for i in [7,6,5]:
        while s_reversed[:i] in words:
            s_reversed = s_reversed[i:]
    if length == len(s_reversed):
        print("NO")
        f = False
        break
if f:
    print("YES")

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

Solution (Python):
s = input()
s = s[::-1]
t = ['maerd', 'remaerd', 'esare', 'resare']

while s:
    l = len(s)
    for i in[7,6,5]:
        while s[:i:] in t:
            s = s[i::]
    if l == len(s):
        print('NO')
        exit()
print('YES')

Solution (Python):
s = input()[::-1]
templates = [s[::-1] for s in ["dream", "dreamer", "erase", "eraser"]]
while s:
    for template in templates:
        if s.startswith(template):
            s = s.replace(template, "", 1)
            break
    else:
        break
print("YES" if not s else "NO")

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

Solution (Python):
s0=input()
s1="maerd"
s2="remaerd"
s3="esare"
s4="resare"
s=""
for i in range(len(s0)):
    s+=s0[-1-i]
num=0
num2=len(s)
ans=0
for i in range(num2):
    if num==num2:
        ans=1
        break
    if num2-num<=4:
        break
    if num2-num<=7:
        s13=s[num:]
        if s13==s1 or s13==s2 or s13==s3 or s13==s4:
            ans=1
        break
    if s[num:num+5]==s1 or s[num:num+5]==s3:
        num+=5
    elif s[num:num+6]==s4:
        num+=6
    elif s[num:num+7]==s2:
        num+=7
    else:
        break
if ans==0:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

S = input()

i = len(S)
ok = True

while ok and i > 0:
    if i - 7 >= 0 and S[i - 7:i] == "dreamer":
        i = i - 7
    elif i - 5 >= 0 and S[i - 5:i] == "dream":
        i = i - 5
    elif i - 6 >= 0 and S[i - 6:i] == "eraser":
        i = i - 6
    elif i - 5 >= 0 and S[i - 5:i] == "erase":
        i = i - 5
    else:
        ok = False

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

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

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

while i != len(S):
    if A[i:i+5] == "maerd":
        i += 5
    elif A[i:i+5] == "esare":
        i += 5
    elif A[i:i+7] == "remaerd":
        i += 7
    elif A[i:i+6] == "resare":
        i += 6
    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 len(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", "")
if len(s) == 0:
    print("YES")
else:
    print("NO")

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

Solution (Python):
def h(temp):
    if temp[0] == "m":
        a = 0
    elif temp[0] == "r":
        if temp[2] == "m":
            a = 1
        else:
            a = 3
    else:
        a = 2
    return a 

def H(temp,y,a):
    c = 0
    if a == 0 or a == 2:
        for j in range(5):
            if y[a][4-j] != temp[0]:
                return c
            temp.remove(temp[0])
        c = 1
    elif a == 3:
        for j in range(6):
            if y[a][5-j] != temp[0]:
                return c
            temp.remove(temp[0])
        c = 1
    else:
        for j in range(7):
            if y[a][6-j] != temp[0]:
                return c
            temp.remove(temp[0])
        c = 1
    return c


s = input()
y = ['dream','dreamer','erase','eraser'] 
temp = []
t = []
k = len(s)
for i in range(k):
    temp.append(s[k-i-1])
c = 1
while len(temp) != 0 and c == 1:
    a = h(temp)
    c = H(temp,y,a)
if c == 1:
    print("YES")
else :
    print("NO")

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

Solution (Python):

import math
s = input()

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

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

Solution (Python):

S = input()
Srev = S[::-1]

Listrev = ["maerd", "remaerd", "esare", "resare"]
can = True
while can == True:
	if Srev == "":
		break
	can = False
	for i in range(4):
		rev=Listrev[i]
		length = len(rev)
		if Srev[0:length] == rev:
			can = True
			Srev=Srev[length:]
if can == True:
	print("YES")
else:
	print("NO")

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

Solution (Python):
S=input()
s=len(S)
T=""
i=0
while True:
    if S[i:i+5]=="dream":
        if i+5==s or S[i+5:i+7]!="er":
            T+="dream"
            i+=5
        elif S[i+5:i+7]=="er":
            if i+7==s or S[i+7]!="a":
                T+="dreamer"
                i+=7
            else:
                T+="dream"
                i+=5
    elif S[i:i+6]=="eraser":
        T+="eraser"
        i+=6
    elif S[i:i+5]=="erase":
        T+="erase"
        i+=5
    else:
        print("NO")
        exit()
    
    if len(T)>=len(S):
        break
print("YES")

Solution (Python):
S = input()

while 1:

    flag = 0
    if S[-5:] == "erase":
        S = S[:-5]
        flag = 1
    if S[-6:] == "eraser":
        S = S[:-6]
        flag = 1
    if S[-5:] == "dream":
        S = S[:-5]
        flag = 1
    if S[-7:] == "dreamer":
        S = S[:-7]
        flag = 1

    if flag == 0:
        break

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

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

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

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

Solution (Python):
S = input().strip()
N = len(S)
cur = 0
flag = 0
while cur<N:
    if S[cur:cur+5]=="dream":
        if cur+5==N:
            cur += 5
        elif cur+5<N and S[cur+5]=="d":
            cur += 5
        elif S[cur:cur+7]=="dreamer":
            if cur+7==N:
                cur += 7
            elif S[cur+7] in ["d","e"]:
                cur += 7
            elif S[cur+7]=="a":
                cur += 5
            else:
                flag = 1
                break
        else:
            flag=1
            break
    elif S[cur:cur+5]=="erase":
        if cur+5==N:
            cur += 5
        elif cur+5<N and S[cur+5]=="r":
            cur += 6
        elif cur+5<N and S[cur+5] in ["d","e"]:
            cur += 5
        else:
            flag = 1
            break
    else:
        flag=1
        break
if flag==0:
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
s = s.replace("dream", "D")
s = s.replace("erase", "E")
s = s.replace("Der", "D")
s = s.replace("Er", "E")

s = list(set(s))
ok = True
for i in range(len(s)):
    if s[i] not in ["D","E"]:
        ok = False
print("YES" if ok else "NO")

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

ans = ""
i = 0
for _ in range(n):
    if i > n or ans == s: break
    if s[i:i+5] == "erase":
        i += 5
        ans += "erase"
        if s[i:i+1] == "r":
            i += 1
            ans += "r"
    elif s[i:i+5] == "dream":
        i += 5
        ans += "dream"
        if s[i:i+3] == "era": continue
        elif s[i:i+2] == "er":
            i += 2
            ans += "er"
print("YES" if ans == s else "NO")

Solution (Python):
S = str(input())
A = "dream"
B = "dreamer"
C = "erase"
D = "eraser"
while len(S) >= 5:
  if len(S) >= 7 and S[-7:] == B:
    S = S[:-7]
  elif len(S) >=6 and S[-6:] == D:
    S = S[:-6]
  elif S[-5:] == A or S[-5:] == C:
      S = S[:-5]
  else:
    break
if len(S) == 0:
  print("YES")
else:
  print("NO")

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

counter = 0

T = len(S)
K = T//5+1

for i in range (0, K+1):
	T = len(S)
	if S[-5::] == 'dream':
		S = S[0:T-5]
	elif S[-5::] == 'erase':
		S = S[0:T-5]
	elif S[-7::] == 'dreamer':
		S = S[0:T-7]
	elif S[-6::] == 'eraser':
		S = S[0:T-6]

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

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

Solution (Python):
S = input()
s = S[::-1]
a = "dream"[::-1]
b = "dreamer"[::-1]
c = "erase"[::-1]
d = "eraser"[::-1]

idx = 0
ans = "YES"
while idx < len(s):
    if s[idx] == "m":
        if s[idx : idx + 5] == a:
            idx = idx + 5
        else:
            ans = "NO"
            break

    elif s[idx] == "e":
        if s[idx : idx + 5] == c:
            idx = idx + 5
        else:
            ans = "NO"
            break

    elif s[idx] == "r":
        if s[idx : idx + 7] == b:
            idx = idx + 7
        elif s[idx : idx + 6] == d:
            idx = idx + 6
        else:
            ans = "NO"
            break

    else:
        ans = "NO"
        break

print(ans)

Solution (Python):
import re
s = input()

if re.match("(dreamer|dream|eraser|erase)+$",s):
  print("YES")
else:
  print("NO")

Solution (Python):
import re
print ('YES' if re.search(r'\A(?:dream(?:er)?|eraser?)+\Z', input()) else 'NO')

Solution (Python):
def main():
    s=input()
    dp=[0]*len(s)
    dp[0]=1
    for i,v in enumerate(s):
        if dp[i] == 0 or i+5 > len(s):
            continue
        s5 = s[i:i+5]
        if s5 == "dream" or s5 == "erase":
            if len(s) == i+5:
                break
            dp[i+5]=1
        if i+6 <= len(s) and s[i:i+6] == "eraser":
            if len(s) == i+6:
                break
            dp[i+6]=1
        if i+5 <= len(s) and s[i:i+7] == "dreamer":
            if len(s) == i+7:
                break
            dp[i+7]=1
    else:
        print("NO")
        return
    print("YES")

if __name__ == "__main__":
    main()

Solution (Python):
import re

def main():
    S = input()
    print('YES' if re.match(r'^(dream|dreamer|erase|eraser)*$', S) else "NO")

main()

Solution (Python):
from sys import exit

s = input()
l = ['dream', 'dreamer', 'erase', 'eraser']

pos = len(s) - 1
while pos >= 0:
    flag = False

    for string in l:
        length = len(string)
        if pos >= length - 1 and s[pos-length+1:pos+1] == string:
            pos -= length
            flag = True

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

print('YES')

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

Solution (Python):
s=input()


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

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

Solution (Python):
S = input()
S_inv = S[::-1]
i=0
flg = True
while i<len(S):
    if S_inv[i:i+5]=='dream'[::-1]:
        i = i + 5
    elif S_inv[i:i+7]=='dreamer'[::-1]:
        i = i + 7
    elif S_inv[i:i+5]=='erase'[::-1]:
        i = i + 5
    elif S_inv[i:i + 6] == 'eraser'[::-1]:
        i = i + 6
    else:
        flg = False
        break

if flg ==True:
    print('YES')
else:
    print('NO')

Solution (Python):
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    s = input()
    word = ['eraser', 'erase', 'dreamer', 'dream']
    for x in word:
        s = s.replace(x, '')
    if s == '':
        print('YES')
    else:
        print('NO')
        
        
        
            
        
    
    
if __name__ == '__main__':
    main()

Solution (Python):
s=input()
l=len(s)
def len_s(l,n):
    return l-n+1

def get_tail(s,n,number):
    if n==0:
        return s[-number:]
    else:
        return s[-number-n:-n]


    
def check(s,n):
    tail=get_tail(s,n,3)
    if tail=="eam" and len_s(l,n)>=5:
        if get_tail(s,n,5)=="dream":
            return True,n+5
        else:
            return False,None
    elif tail=="mer" and len_s(l,n)>=7:
        if get_tail(s,n,7)=="dreamer":
            return True,n+7
        else:
            return False,None
    elif tail=="ase" and len_s(l,n)>=5:
        if get_tail(s,n,5)=="erase":
            return True,n+5
        else:
            return False,None
    elif tail=="ser" and len_s(l,n)>=6:
        if get_tail(s,n,6)=="eraser":
            return True,n+6
        else:
            return False,None
    else:
        return False,None
ans=0
tail=0
while True:
    bool,tail=check(s,tail)
    if bool:
        if tail==l:
            break
    else:
        ans+=1
        break   
if ans==0:
    print("YES")
else:
    print("NO")