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):
def main():
  s = input()
  dream = False
  erase = False

  while s:
    if s.startswith('dream'):
      s = s[5:]
      dream = True
      erase = False
    elif s.startswith('erase'):
      s = s[5:]
      dream = False
      erase = True
    elif s.startswith('er') and dream:
      s = s[2:]
      dream = False
    elif s.startswith('r') and erase:
      s = s[1:]
      erase = False
    else:
      return False
  
  return True

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

Solution (Python):



def main():
    s = input()

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

    flg = True
    while len(s) > 0:
        for koho in koho_list:
            if len(s) >= len(koho) and s[-len(koho):] == koho:
                s = s[:-len(koho)]
                break
        else:
            flg = False
            break

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


if __name__ == '__main__':
    main()

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

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

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

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

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

print(answer)

Solution (Python):
text = "".join(reversed(input()))
words = ["".join(reversed(x)) for x in ["dreamer", "eraser", "dream", "erase"]]

while text != "":
  has_striped = False
  for word in words:
    if text.find(word) != 0: continue
    text = text[len(word):]
    has_striped = True
  if not has_striped: break

print("YES" if text == "" else "NO")

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    S = input()
    S = S[::-1]
    ans = ["maerd", "remaerd", "esare", "resare"]
    while S:
        for a in ans:
            if S[:5] in ans:
                S = S.replace(S[:5], "", 1)
                break
            elif S[:6] in ans:
                S = S.replace(S[:6], "", 1)
                break
            elif S[:7] in ans:
                S = S.replace(S[:7], "", 1)
                break
        else:
            return print("NO")
    else:
        return print("YES")


if __name__ == '__main__':
    main()

Solution (Python):
S = list(input())
N = len(S)
c = 0
dream = list("dream")
dreamer = list("dreamer")
erase = list("erase")
eraser = list("eraser")
while True:
    if S[c:c + 5] == dream:
        c += 5
        if S[c:c + 5] != erase and S[c:c + 2] == list("er"):
            c += 2
        if c == N:
            print("YES")
            break
    elif S[c:c + 5] == erase:
        c += 5
        if S[c:c + 1] == ["r"]:
            c += 1
        if c == N:
            print("YES")
            break
    else:
        print("NO")
        break

Solution (Python):
S=input()
T=["dream","dreamer","erase","eraser"]
S=S.replace(T[3],"0000000").replace(T[2],"00000").replace(T[1],"0000000").replace(T[0],"00000")
U=""
for k in range(len(S)):
    U+="0"
if S==U:
    print("YES")
else:
    print("NO")

Solution (Python):

import sys 
def main():
    s = input()
    s = s.replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')
    if s == '':
        print('YES')
    else:
        print('NO')

if __name__ == '__main__' :
    main()

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

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

cur_pos = 0
tmp_str = ''
str_list = ['maerd', 'remaerd', 'esare', 'resare']

while (cur_pos < N):
    tmp_str += S[cur_pos]

    if (tmp_str in str_list):
        tmp_str = ''
    
    cur_pos += 1

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

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

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

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

Solution (Python):
s = input()
ans = "YES"

while (len(s) - 5) > 2:
    c = s[-5:]
    if (c == "dream"):
        s = s[ : -5]
    else:
        if (c == "erase"):
            s = s[: -5]
        else:
            c = s[-6:]
            if (c == "eraser"):
                s = s[: -6]
            else:
                c = s[-7:]
                if (c == "dreamer"):
                    s = s[: -7]
                else:
                    ans = "NO"
                    break

                    
if s != "dream":
    if s != "erase":
        if s != "dreamer":
            if s != "eraser":
                ans = "NO"

print(ans)

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

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

Solution (Python):
s = input()
search = ['dream', 'dreamer', 'erase', 'eraser']
index = len(s)
while index > 0:
  found = False
  for i in search:
    if index < len(i):
      continue
    if s[index-len(i):index] == i:
      found = True
      index -= len(i)
      break
  if not found:
    print('NO')
    exit()
print('YES')

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

Solution (Python):
s = input()[::-1]
n = len(s)
i = 0
ans = 'YES'
while i < n:
    if s[i] == 'm':
        if n - i < 5:
            ans = 'NO'
            break
        if s[i:i+5] == 'maerd':
            i += 5
            continue
        else:
            ans = 'NO'
            break
    elif s[i] == 'e':
        if n - i < 5:
            ans = 'NO'
            break
        if s[i:i+5] == 'esare':
            i += 5
            continue
        else:
            ans = 'NO'
            break
    elif s[i] == 'r':
        if n - i < 6:
            ans = 'NO'
            break
        if s[i:i+6] == 'resare':
            i += 6
            continue
        else:
            if n - i < 7:
                ans = 'NO'
                break
            if s[i:i+7] == 'remaerd':
                i += 7
                continue
            else:
                ans = 'NO'
                break
    else:
        ans = 'NO'
        break
print(ans)

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):
def main():
    S = input()
    while len(S):
        if S[:7] == 'dreamer':
            if S[5:11] == 'eraser':
                S = S[5:]
            elif S[5:10] == 'erase':
                S = S[5:]
            else:
                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')
            exit()
    print('YES')


if __name__ == '__main__':
    main()

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()[::-1]
while True:
    if len(s) == 0:
        print ("YES")
        exit ()

    if s[0:5] == "maerd" or s[0:5] == "esare":
        s = s[5:]
    else:
        if s[0:6] == "resare":
            s = s[6:]
        else:
            if s[0:7] == "remaerd":
                s = s[7:]
            else:
                print ("NO")
                exit ()

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")
        break
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('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):
s=list(str(input()))
b=0
while len(s)>=7:
  if s[-1]=='r':
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0 or (len(s)==6 and ''.join(s)=='eraser') or (len(s)==5 and ''.join(s)=='dream') or (len(s)==5 and ''.join(s)=='erase'):
  print('YES') 
elif b!=1:
  print('NO')

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

Solution (Python):
a = str(input())
l = len(a)
let5 = ['dream','erase']


while l >= 5:
  lb = len(a)
  if a[-5:] in let5:
    a = a[:-5]
    l -= 5
  if a[-6:] == 'eraser':
    a = a[:-6]
    l -= 6
  if a[-7:] == 'dreamer':
    a = a[:-7]
    l -= 7
  if l == 0:
    print('YES')
    break
  if l == lb:
    print('NO')
    break
else:
  print('NO')

Solution (Python):
S = input()


c1 = S.count('dream')  
c2 = S.count('dreamer')  
c3 = S.count('erase')  
c4 = S.count('eraser')  

c6 = S.count('dreamerase')  

num_eraser = c4
num_erase = c3 - num_eraser
num_dreamer = c2 - c6
num_dream = c1 - num_dreamer

l = num_eraser * 6 + num_erase * 5 + num_dreamer * 7 + num_dream * 5
if l == len(S):
    print('YES')
else:
    print('NO')

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

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

Solution (Python):
S = input()
S = S[::-1]
pattern = ["dream","dreamer","erase","eraser"]
for i in range(len(pattern)):
  pattern[i] = pattern[i][::-1]
while True:
  flag = False
  for v in pattern:
    if S[:len(v)] == v:
      S = S[len(v):]
      flag = True
  if S == "":
    print("YES")
    break
  elif not flag:
    print("NO")
    break

Solution (Python):



def main():
    S = input()
    S = S[::-1]
    a = ['maerd', 'remaerd', 'esare', 'resare']
    if S.replace(a[3], '').replace(a[2], '').replace(a[1], '').replace(a[0], '') == "":
        print('YES')
    else:
        print('NO')

main()

Solution (Python):
S = input()



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

while True:
    flag = False

    for word in words:
        
        length = len(word)
        if S[-length:] == word:
            S = S[:len(S) - length]
            flag = True
            break

    if flag:
        if S == '':
            print('YES')
            break
        else:
            continue
    else:
        print('NO')
        break

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

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

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

Solution (Python):
s = input()

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

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

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

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

while s:
    if s.rfind('dream') == l-len('dream'): 
        s = s[:l-len('dream')] 
    
    elif s.rfind('dreamer') == l-len('dreamer'):
        s = s[:l-len('dreamer')]

    elif s.rfind('erase') == l-len('erase'):
        s = s[:l-len('erase')]
    
    elif s.rfind('eraser') == l-len('eraser'):
        s = s[:l-len('eraser')]

    else:
        break 

    l = len(s)

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

Solution (Python):
import sys

sys.setrecursionlimit(20000+1)

def main():
    s = "".join(list(reversed(str(input()))))
    b = True
    
    while len(s) != 0:
        if len(s) >= 5:
            use = ["maerd","esare"]
            if s[:5] in use:
                s = s[5:]
            elif (len(s) >= 6 and s[:6] == "resare"):
                s = s[6:]
            elif (len(s) >= 7 and s[:7] == "remaerd"):
                s = s[7:]
            else:
                b = False
                break
        else:
            b = False
            break
            
    if b:
        print("YES")
    else:
        print("NO")

if __name__ == "__main__":
    main()

Solution (Python):
S = input()

words = ["dream", "dreamer", "erase", "eraser"]
ans = "YES"
r = len(S)

while r > 0:
    ok = False
    for w in words:
        l = r - len(w)
        if l < 0:
            continue
        if S[l:r] == w:
            r = l
            ok = True
            break
    if not ok:
        ans = "NO"
        break

print(ans)

Solution (Python):
s = input()
while s:
    if s.startswith("eraser"):
        s = s[6:]
    elif s.startswith("erase"):
        s = s[5:]
    elif s.startswith("dreamera"):
        s = s[5:]
    elif s.startswith("dreamer"):
        s = s[7:]
    elif s.startswith("dream"):
        s = s[5:]
    else:
        print("NO")
        break
else:
    print("YES")

Solution (Python):
s = input()
temp = s
while len(temp) > 4:
    if temp[len(temp) - 3:] == 'eam' and temp[len(temp) - 5:] == 'dream': temp = temp[: len(temp) - 5]
    elif temp[len(temp) - 3:] == 'mer' and temp[len(temp) - 7:] == 'dreamer': temp = temp[: len(temp) - 7]
    elif temp[len(temp) - 3:] == 'ase' and temp[len(temp) - 5:] == 'erase': temp = temp[: len(temp) - 5]
    elif temp[len(temp) - 3:] == 'ser' and temp[len(temp) - 6:] == 'eraser': temp = temp[: len(temp) - 6]
    else: break
if len(temp) > 4: print('NO')
else: print('YES')

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

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

f = 0

if len(s) == 0:
    f = 1

while len(s) != 0:
    if s[:7] == 'remaerd':
        s = s[7::]
    elif s[:6] == 'resare':
        s = s[6::]
    elif s[:5] == 'esare':
        s = s[5:]
    elif s[:5] == 'maerd':
        s = s[5:]
    else:
        f = 1
        break

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

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

s = readline().rstrip().decode('utf-8')
s = s[::-1]
n = len(s)
l = ["dreamer","dream","eraser", "erase"]
for i in range(4):
    l[i] = l[i][::-1]

i=0
while i < n:
    flag = 0
    for j in range(4):
        if i+len(l[j]) <= n and s[i:i+len(l[j])] == l[j]:
            i += len(l[j])
            flag = 1
            break
    if not flag:
        print("NO")
        exit()
print("YES")

Solution (Python):

S = input()  




answer = 'YES'

while True:
    tester = S[-3:] 
    if(tester == 'eam'): 
        if(S[-5:] == 'dream'): 
            S = S[:-5] 
            
            
        else:
            answer = 'NO'
            break
    elif(tester == 'mer'): 
        if(S[-7:] == 'dreamer'): 
            S = S[:-7] 
            
            
        else:
            answer = 'NO'
            break
    elif(tester == 'ase'): 
        if(S[-5:] == 'erase'): 
            S = S[:-5] 
            
            
        else:
            answer = 'NO'
            break
    elif(tester == 'ser'): 
        if(S[-6:] == 'eraser'): 
            S = S[:-6] 
            
            
        else:
            answer = 'NO'
            break
    elif(len(S) == 0):
        break
    else:
        answer = 'NO'
        break

print(answer)

Solution (Python):

S = str(input())

ans = 'YES'
while len(S)>0:
  if S[-5:] == 'dream':
    S = S[:-5]
  elif 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):

s=input()
words = {"dream", "dreamer", "erase", "eraser"}

dp = dict()
dp[0] = 1
for i in range(0, len(s)):
    if not dp.get(i):
        continue
    for word in words:
        if s[i:i+len(word)] == word:
            dp[i+len(word)] = 1

print("YES" if dp.get(len(s)) else "NO")

Solution (Python):
s = input()

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

import re
def setudan(s):
  if s[-5:] == 'dream' or s[-5:] == 'erase':
    return s[:-5]
  elif s[-6:] == 'eraser':
    return s[:-6]
  elif s[-7:] == 'dreamer':
    return s[:-7]
  else:
    return 0

while True:
  s = setudan(s)
  if s == 0:
    print('NO')
    break
  if s =='':
    print('YES')
    break

Solution (Python):
S0 = input()
S = S0[::-1]
cands = ["dream"[::-1],"dreamer"[::-1],"erase"[::-1],"eraser"[::-1]]
i = 0
while i < len(S):
  f = False
  for word in cands:
    
    if S[i:].startswith(word):
        f = True
        i += len(word)
        break
  if f == False:
    print("NO")
    exit(0)

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

Solution (Python):
S = input()

ind = len(S)
flg = True
while ind > 0:
    if S[ind-7:ind] in ["dreamer"]:
        ind -= 7
    elif S[ind-6:ind] in ["eraser"]:
        ind -= 6
    elif S[ind-5:ind] in ["dream", "erase"]:
        ind -= 5
    else:
        flg = False
        break

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

Solution (Python):
hantei = ["dream", "dreamer", "erase", "eraser"]
s = input()
len_s = len(s)
s = s+"0000000000000"

checker = 1
i = 0
while i < len_s:
    if s[i:i+5] == "dream":
        if s[i+5:i+7] == "er":
            if s[i+7] =="a":
                i += 5
            else:
                i += 7
        else:
            i += 5
    elif s[i:i+5] == "erase":
        if s[i+5] == "r":
            i += 6
        else:
            i += 5
    else:
        checker = 0
        break
if checker:
    print("YES")
else:
    print("NO")

Solution (Python):





S = input()

i = len(S)
while i - 3 > 0:
    tri = S[i - 3:i]
    if tri == 'eam':
        is_ok = S[i - 5:i] == 'dream'
        i -= 5
    elif tri == 'ase':
        is_ok = S[i - 5:i] == 'erase'
        i -= 5
    elif tri == 'mer':
        is_ok = S[i - 7:i] == 'dreamer'
        i -= 7
    elif tri == 'ser':
        is_ok = S[i - 6:i] == 'eraser'
        i -= 6
    else:
        print('NO')
        exit()

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


print('YES')

Solution (Python):





S = input()

i = len(S)
T = ''
while i - 3 > 0:
    tri = S[i - 3:i]
    if tri == 'eam':
        T += 'maerd'
        i -= 5
    elif tri == 'ase':
        T += 'esare'
        i -= 5
    elif tri == 'mer':
        T += 'remaerd'
        i -= 7
    elif tri == 'ser':
        T += 'resare'
        i -= 6
    else:
        print('NO')
        exit()

if S == T[::-1]:
    print('YES')
else:
    print('NO')

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

a = ("dream", "dreamer", "erase", "eraser")

while True:
    if S == "":
        break
    flg = False
    for aa in a:
        if S.endswith(aa):
            S = S[:-len(aa)]
            flg = True
    if flg:
        continue
    break

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

Solution (Python):
S = input()

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

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

Solution (Python):


S = input()
N = len(S)

S = S[::-1]

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

while len(S) > 0:
    if 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')
        break
if len(S) == 0:
    print('YES')

Solution (Python):
S = input()

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

current = S
canEqual = False
while len(current) > 0:
  matchExists = False
  for w in words:
    last_index = -len(w)
    if(current[last_index:] == w):
      current = current[:last_index]
      matchExists = True
      break
  
  if(len(current) == 0):
    canEqual = True
    break
  
  if(matchExists == False):
    break

result = "YES" if canEqual == True else "NO"
print(result)

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))
MOD = 10 ** 9 + 7


S = input()
S = ''.join(list(reversed(S)))
while len(S) > 0:
    if S[:7] == 'remaerd':
        S = S[7:]
    elif S[:6] == 'resare':
        S = S[6:]
    elif S[:5] in ['esare', 'maerd']:
        S = S[5:]
    else:
        print('NO')
        exit(0)
print('YES')

Solution (Python):
S = str(input())
S_iterator = reversed(S)
S_list = list(S_iterator)
s1 = ['m','a','e','r','d']
s2 = ['r','e','m','a','e','r','d']
s3 = ['e','s','a','r','e']
s4 = ['r','e','s','a','r','e']

i=0
while(i < len(S_list)):
    if(S_list[i:i+5]==s1):
        err = 0
        i = i+5
    elif(S_list[i:i+7]==s2):
        err = 0
        i = i+7
    elif(S_list[i:i+5]==s3):
        err = 0
        i = i+5
    elif(S_list[i:i+6]==s4):
        err = 0
        i = i+6
    else:
        err = 1
        break
    
if(err == 0):
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
s = str(input())
i = 0
while i < len(s):
    if i + 5 <= len(s) and s[i:i+5] == 'dream': 
        if i + 6 < len(s) and s[i+5] == 'e' and s[i+6] == 'r': 
            if i + 7 < len(s) and s[i+7] == 'a': 
                i += 5
            else: 
                i += 7
        else: 
            i += 5
    elif i + 5 <= len(s) and s[i:i+5] == 'erase':
        if i + 5 < len(s) and s[i+5] == 'r': 
            i += 6
        else: 
            i += 5
    else:
        print('NO')
        sys.exit()
print('YES')

Solution (Python):
s = input()
now = 0
n = len(s)
prev = ""
x = {"eraser", "dreamer", "dream", "erase"}
y = {"ase", "aser"}
ds = [7, 6, 5, 4, 3]
ans = "YES"
while now < n:
    q = 1
    for i in ds:
        index = now + i
        if index > n:
            continue
        w = s[now:index]
        if w in x:
            now += i
            prev = w
            q = 0
            break
        elif prev == "dreamer" and w in y:
            now += i
            prev = w
            q = 0
            break
    if q:
        ans = "NO"
        break
print(ans)

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

while len(s)!=0:
    for i in words:
        l = len(i)
        if s[0:l] == i[::-1]:
            s = s[l::]
            break
    else:
        break

print("NO"if len(s)!=0else "YES")

Solution (Python):

s = str(input())
N = len(s)

while s != "":
    if s[N-5:N] == "dream" or s[N-5:N] == "erase":
        s = s[:N-5]
        N = len(s)
    elif s[N-6:N] == "eraser":
        s = s[:N-6]
        N = len(s)
    elif s[N-7:N] == "dreamer":
        s = s[:N-7]
        N = len(s)
    else:
        break

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

Solution (Python):
def main():
    target = input()
    string = ["eraser", "dreamer", "erase", "dream"]
    for i in range(len(target)):
        for s in string:
            if target[-len(s):] == s:
                target = target[:-len(s)]
        if target == "":
            break
    print("YES" if target == "" else "NO")


if __name__ == '__main__':
    main()

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

Solution (Python):
s = input()
t = ""

p = 0
while p < len(s):
    if s[p:p+5] == 'dream' and s[p+5:p+6] != 'e':
        p += 5
        t += 'dream'
    elif s[p:p+5] == 'erase' and s[p+5:p+6] != 'r':
        p += 5
        t += 'erase'
    elif s[p:p+6] == 'eraser':
        p += 6
        t += 'eraser'
    elif s[p:p+7] == 'dreamer' and s[p+7:p+8] != 'a':
        p += 7
        t += 'dreamer'
    elif s[p:p+7] == 'dreamer' and s[p+7:p+8] == 'a':
        p += 5
        t += 'dream'
    else:
        break

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

Solution (Python):
def main():
    s=input()[::-1]
    string=('maerd','remaerd','esare','resare')
    
    n=len(s)
    i=0
    while i<n:
        st=s[i:i+5]
        if st in string:
            i+=5
            continue
        st=s[i:i+6]
        if st==string[3]:
            i+=6
            continue
        st=s[i:i+7]
        if st==string[1]:
            i+=7
            continue
        else:
            print('NO')
            exit()
    print('YES')   

if  __name__=='__main__':
    main()

Solution (Python):
def main():
    s=input()
    string=('dream','erase')

    i=0
    n=len(s)
    while i<n:
        st=s[i:i+5]
        if st==string[0]:
            i+=5
            st=s[i:i+2]
            if st=='er':
                if i+2==n:
                    break
                if s[i+2]!='a':
                    i+=2
        
        elif st==string[1]:
            i+=5
            if i==n:
                break
            if s[i]=='r':
                i+=1
        
        else:
            print('NO')
            exit()
    print('YES')
if  __name__=='__main__':
    main()

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

i = 0
while i < n:
    if s[i:i+5] == 'maerd':
        i += 5
    elif s[i:i+7] == 'remaerd':
        i += 7
    elif s[i:i+5] == 'esare':
        i += 5
    elif s[i:i+6] == 'resare':
        i += 6
    else:
        print('NO')
        break
else:
    print('YES')

Solution (Python):
import numpy as np

d = np.array([[0, 0, 0, 0, 0, 0],
            [2, 0, 9, 0, 0, 0],
            [0, 3, 0, 0, 0, 0],
            [0, 0, 4, 0, 0, 0],
            [0, 0, 0, 5, 0, 0],
            [0, 0, 0, 0, 6, 0],
            [2, 0, 7, 0, 0, 0],
            [0, 8, 0, 0, 0, 0],
            [2, 0, 9, 11, 0, 0],
            [0, 10, 0, 0, 0, 0],
            [0, 0, 0, 11, 0, 0],
            [0, 0, 0, 0, 0, 12],
            [0, 0, 13, 0, 0, 0],
            [2, 1, 9, 0, 0, 0]], dtype = 'int32')
s = 1
S = input()
N = len(S)
id = {'d': 0, 'r': 1, 'e': 2, 'a': 3, 'm': 4, 's': 5}
for c in S:
    s = d[s][id[c]] if c in id else 0
    if s == 0:
        break

print("YES" if s in {1, 6, 8, 13} else "NO")

Solution (Python):
s=input()
n=len(s)
s=s+"0000000"
c=0
check=0
while c<n:
    if s[c:c+5]=="dream":
        if s[c+5:c+7]=="er":
            if s[c+7]=="a":
                c+=5
            else:
                c+=7
        else:
            c+=5
    elif s[c:c+5]=="erase":
        if s[c+5]=="r":
            c+=6
        else:
            c+=5
    else:
        check+=1
        break
if check==0:
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
while len(s) > 4:
    if s[:5] == "dream":
        if s[5:7] == "er":
            if s[7:10] == "ase":
                s = s[5:]
            else:
                s = s[7:]
        else:
            s = s[5:]
    elif s[:5] == "erase":
        if s[5:6] == "r":
            s = s[6:]
        else:
            s = s[5:]
    else:
        break
if s == "":
    print("YES")
else:
    print("NO")

Solution (Python):
def resolve():
    S = list(input())
    S.reverse()
    substr = ''
    p = ['dream', 'dreamer', 'erase', 'eraser']
    for i in S:
        substr = i + substr
        if substr in p:
            substr = ''
    ans = 'YES' if substr == '' else 'NO'
    print(ans)


if __name__ == "__main__":
    resolve()

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