Hello! Is there a way you can optimize my python code? 
n=int(input())
s=input()
nach=0

flashok=False
mass=[]
for J in range(n):
    if s[J] == 'X':
        nach = len(mass)
        mass.append('.')
        flashok=False
    elif s[J]=='.':
        mass.append(s[J])
        flashok=False
    elif not(flashok):
        mass.append('7')
        flashok=True
def otvet(mass, nach):
    if nach==len(mass)-1:
        return True
    z1=0
    z2=0
    flag1 = False
    flag2 = False
    if nach!=0 and mass[nach-1]!='.':
        for z1 in range(nach-2,-1,-1):
            if mass[z1]=='.':
                flag1 = True
                mass[nach]='1'
                break

    if mass[nach+1]!='.':
        for z2 in range(nach+2,len(mass)):
            if mass[z2]==".":
                flag2=True
                mass[nach]='1'
                break
    boolik=False
    if flag1 and flag2:
        boolik=otvet(mass,z1) or otvet(mass, z2)
    elif flag1:
        boolik=otvet(mass, z1)
    elif flag2:
        boolik=otvet(mass, z2)
    mass[nach]='.'
    return boolik

if nach==len(mass)-1:
    print("YES")
elif mass[len(mass)-1]=='.':
    BEPPIEV=otvet(mass, nach)
    if BEPPIEV:
        print("YES")
    if not(BEPPIEV):
        print("NO")
else:
    print("NO")

print(mass)
