Problem p03854 (python)

Fixed submission:
S = input()
S = S[::-1]
#print(S)

while True:
    if len(S) >= 7 and S[:7] =='remaerd':
        S = S[7:]
    elif len(S) >= 6 and S[:6] =='resare':
        S = S[6:]
    elif len(S) >= 5 and S[:5] =='maerd':
        S = S[5:]
    elif len(S) >= 5 and S[:5] =='esare':
        S = S[5:]
    else:
        print("NO")
        exit()
    if len(S) == 0:
        print("YES")
        exit()

Buggy submission (verdict: Runtime Error):
S=input()
lenS=len(S)
L=[0]*10000
j=0
i=0
s=0
S+="######################"
while(True):
    if(lenS==i):
        print("YES")
        exit()
    if(S[i:i+7]=="dreamer" and s<1):
        L[j]=1
        j+=1
        i+=7
        s=0
        continue
    if(S[i:i+5]=="dream" and s<2):
        L[j]=2
        j+=1
        i+=5
        s=0
        continue
    if(S[i:i+6]=="eraser" and s<3):
        L[j]=3
        j+=1
        i+=6
        s=0
        continue
    if(S[i:i+5]=="erase" and s<4):
        L[j]=4
        j+=1
        i+=5
        s=0
        continue
    j-=1
    if(j<0):
        print("NO")
        exit()
    if(L[j]==1):
        i-=7
        s=1
    elif(L[j]==2):
        i-=5
        s=2
    elif(L[j]==3):
        i-=6
        s=3    
    elif(L[j]==4):
        i-=5
        s=4

Buggy submission (verdict: Runtime Error):
S=input()
lenS=len(S)
L=[0]*10000
j=0
i=0
s=0
S+="######################"
while(True):
    if(lenS==i):
        print("YES")
        exit()
    if(S[i:i+7]=="dreamer" and s<1):
        L[j]=1
        j+=1
        i+=7
        s=0
        continue
    if(S[i:i+5]=="dream" and s<2):
        L[j]=2
        j+=1
        i+=5
        s=0
        continue
    if(S[i:i+6]=="eraser" and s<3):
        L[j]=3
        j+=1
        i+=6
        s=0
        continue
    if(S[i:i+5]=="erase" and s<4):
        L[j]=4
        j+=1
        i+=5
        s=0
        continue
    j-=1
    if(j<0):
        print("NO")
        exit()
    if(L[j]==1):
        i-=7
        s=1
    elif(L[j]==2):
        i-=5
        s=2
    elif(L[j]==3):
        i-=6
        s=3    
    elif(L[j]==4):
        i-=5
        s=4

Buggy submission (verdict: Runtime Error):
S=input()
lenS=len(S)
L=[0]*10000
j=0
i=0
s=0
while(True):
    if(lenS==i):
        print("YES")
        exit()
    if(S[i:i+7]=="dreamer" and s<1):
        L[j]=1
        j+=1
        i+=7
        s=0
        continue
    if(S[i:i+5]=="dream" and s<2):
        L[j]=2
        j+=1
        i+=5
        s=0
        continue
    if(S[i:i+6]=="eraser" and s<3):
        L[j]=3
        j+=1
        i+=6
        s=0
        continue
    if(S[i:i+5]=="erase" and s<4):
        L[j]=4
        j+=1
        i+=5
        s=0
        continue
    j-=1
    if(j<0):
        print("NO")
        exit()
    if(L[j]==1):
        i-=7
        s=1
    elif(L[j]==2):
        i-=5
        s=2
    elif(L[j]==3):
        i-=6
        s=3    
    elif(L[j]==4):
        i-=5
        s=4

Buggy submission (verdict: Runtime Error):
S=input()
lenS=len(S)

def state(i):
    if(lenS==i):
        return True

    if(S[i:i+7]=="dreamer"):
        j=state(i+7)
        if(j==True):
            return True
    if(S[i:i+5]=="dream"):
        j=state(i+5)
        if(j==True):
            return True
    if(S[i:i+6]=="eraser"):
        j=state(i+6)
        if(j==True):
            return True      
    if(S[i:i+5]=="erase"):
        j=state(i+5)  
        if(j==True):
            return True
    return False
i=0
if(state(i)):
    print("YES")
else:
    print("NO")