Problem p03800 (python)

Fixed submission:
n=int(input())
s=input()

def cali(string):
    lists=[0 for i in range(n)]
    lists[0]=string[0]
    lists[1]=string[1]
    flag1=False
    flag2=False
    for  _ in range(2,n):
        if s[_-1]=="o" and lists[_-2]=="W" and lists[_-1]=="W":
            lists[_]="S"
            
        elif s[_-1]=="o" and lists[_-2]=="W" and lists[_-1]=="S":
            lists[_]="W"
            
        elif s[_-1]=="o" and lists[_-2]=="S" and lists[_-1]=="W":
            lists[_]="W"
            
        elif s[_-1]=="o" and lists[_-2]=="S" and lists[_-1]=="S":
            lists[_]="S"        
            
        elif s[_-1]=="x" and lists[_-2]=="W" and lists[_-1]=="W":   
            lists[_]="W"
            
        elif s[_-1]=="x" and lists[_-2]=="W" and lists[_-1]=="S":
            lists[_]="S"
        
        elif s[_-1]=="x" and lists[_-2]=="S" and lists[_-1]=="W":
            lists[_]="S"
            
        elif s[_-1]=="x" and lists[_-2]=="S" and lists[_-1]=="S":   
            lists[_]="W"
    

    if s[0]=="o" and lists[0]=="W":
        if lists[1]!=lists[-1]:
            flag1=True

    elif s[0]=="o" and lists[0]=="S":
        if lists[1]==lists[-1]:
            flag1=True
     
    
    elif s[0]=="x" and lists[0]=="W":
        if lists[1]==lists[-1]:
            flag1=True

    elif s[0]=="x" and lists[0]=="S":
        if lists[1]!=lists[-1]:
            flag1=True

    if s[-1]=="o" and lists[-1]=="W":
        if lists[-2]!=lists[0]:
            flag2=True
    elif s[-1]=="o" and lists[-1]=="S":
        if lists[-2]==lists[0]:
            flag2=True

    elif s[-1]=="x" and lists[-1]=="W":
        if lists[-2]==lists[0]:
            flag2=True
    elif s[-1]=="x" and lists[-1]=="S":
        if lists[-2]!=lists[0]:
            flag2=True
    if flag1 and flag2:
         return lists
    else:
          return 0
        

stri=""
a=cali("WW")
b=cali("WS")
c=cali("SW")
d=cali("SS")

if a!=0:
    for i in range(n):
        stri+=a[i]
    print(stri)

elif b!=0:
        for i in range(n):
            stri+=b[i]
        print(stri)
        
elif c!=0:
        for i in range(n):
            stri+=c[i]
        print(stri)
        
elif d!=0:
        for i in range(n):
            stri+=d[i]
        print(stri)
else:
    print("-1")

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
flag=True
lists=["WWW","SWS","WSS","SSW","SWW","WWS","SSS","WSW"]


strings=""
if s[0]=="x":
    j==0
    while j<4:
        strings=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W"   
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"   
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"           
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"
            else:
                break
        if len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break  
        else:
            strings=""
            j+=1
elif s[0]=="o":
    j==4
    while j<8:
        strings=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W" 
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"  
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"
            else:
                break
        if  len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break
        else:
            strings=""
            j+=1    
if flag==True:
    print("-1")

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
flag=True
lists=["WWW","SWS","WSS","SSW","SWW","WWS","SSS","WSW"]
strings=""
if s[0]=="x":
    j==0
    while j<4:
        strings+=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W"   
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"   
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"           
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"

        if len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break  
        else:
            strings=""
            j+=1
elif s[0]=="o":
    j==4
    while j<8:
        strings=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W" 
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"  
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"
        if  len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break
        else:
            strings=""
            j+=1    
if flag==True:
    print("-1")

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
flag=True
lists=["WWW","SWS","WSS","SSW","SWW","WWS","SSS","WSW"]
strings=""
if s[0]=="x":
    j==0
    while j<4:
        strings+=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W"   
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"   
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"           
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"

        if len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break  
        else:
            j+=1
elif s[0]=="o":
    j==4
    while j<8:
        strings=lists[j]
        for i in range(n-1):
            if strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="W" and strings[-1]=="W" and s[i+1]=="x": 
                strings+="W" 
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="o":
                strings=""
                break
            elif strings[-2]=="W" and strings[-1]=="S" and s[i+1]=="x":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="o":
                strings+="S"
            elif strings[-2]=="S" and strings[-1]=="S" and s[i+1]=="x": 
                strings+="W"  
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="o":
                strings+="W"
            elif strings[-2]=="S" and strings[-1]=="W" and s[i+1]=="x":    
                strings+="S"

        if  len(strings)==n+2 and strings[0]==strings[-2] and strings[1]==strings[-1]:
            flag=False
            print(strings[1:-1])
            strings=""
            break
        else:
            j+=1
            
if flag==True:
    print("-1")