Problem p03863 (python)

Fixed submission:
s=input()
def result(limit):
    if limit%2==1:return 'First'
    if limit%2==0:return 'Second'
first=s[0]
last=s[-1]
if first!=last:
    first_idx=[]
    last_idx=[]
    for x in range(s.count(first)):
        first_idx.append(s.index(first,x))
    for y in range(s.count(last)):
        last_idx.append(s.index(last,y))
    judge=0
    data=[]
    data.append(last_idx.pop())
    while judge<2:
        if judge==0:
            while judge==0:
                if len(first_idx)==0:judge=2
                else:
                    k=first_idx.pop()
                    if k<data[-1]:
                        judge=1
                        data.append(k)
                    
        elif judge==1:
            while judge==1:
                if len(last_idx)==0:judge=2
                else:
                    k=last_idx.pop()
                    if k<data[-1]:
                        judge=0
                        data.append(k)
        
    print(result(len(s)-len(data)))

elif first==last:
    first_idx=[]
    for x in range(s.count(first)):
        first_idx.append(s.index(first,x))
    
    alpha='abcdefghijklmnopqrstuvwxyz'
    counter=[0 for _ in range(len(alpha))]
    for i in range(1,len(first_idx)):
        string=s[first_idx[i-1]+1:first_idx[i]]
        for j in range(len(alpha)):
            if alpha[j] in string:counter[j]+=1
    k = max(counter)
    print(result(len(s)-2*k-1))

Buggy submission (verdict: Runtime Error):
s=input()

def check(s):
    if len(s)==2:return []
    ans=[]
    for i in range(2,len(s)):
        if s[i-2]!=s[i]:ans.append(i-1)
    return ans

def result(limit):
    if limit%2==1:return 'Second'
    if limit%2==0:return 'First'

def go(s,now,limit,past,prev):
    if prev==0:branch[now]=check(s)
    if branch[now]==[]:
        if now==1 and prev!=0:print(result(limit))
        elif now==1 and prev==0:print('Second')
        elif now==2:print('First')
        else:go(past[now-2],now-2,now,past[:-2],1)
    else:
        if now+1==limit:go(past[now-1],now-1,limit,past[:-1],1)
        else:
            x=branch[now].pop()
            s=s[:x]+s[x+1:]
            past+=(s,)
            go(s,now+1,limit,past,0)

def calc(s):
    data=[0 for i in range(len(s))]
    log = {}
    for i in range(len(s)):
        if s[i] in log:
            for j in range(log[s[i]]+1,i):
                data[j]+=1
            log[s[i]]=i
        elif s[i] not in log:
            log[s[i]]=i
    return data


def first(s,first_limit):
    first_branch=check(s)
    if first_branch==[]:
        if first_limit==1:return first_limit+1
        else: return first_limit
    data=calc(s)
    data_can=[]
    for i in range(len(data)):
        if i in first_branch:
            data_can.append(data[i])
        else:data_can.append(0)
    x=data_can[1:-1].index(max(data_can))
    s=s[:x]+s[x+1:]
    first_limit+=1
    return first(s,first_limit)
            
branch={}
branch[1]=check(s)
past=(s,)
now=1
limit=first(s,1)
go(s,now,limit,past,0)

Buggy submission (verdict: Runtime Error):
s=input()


def check(s):
    if len(s)==2:return []
    ans=[]
    for i in range(2,len(s)):
        if s[i-2]!=s[i]:ans.append(i-1)
    return ans
def result(limit):
    if limit%2==1:return 'Second'
    if limit%2==0:return 'First'

def go(s,now,limit,past,prev):
    if prev==0:branch[now]=check(s)
    if branch[now]==[]:
        if now==1 and prev!=0:print(result(limit))
        elif now==1 and prev==0:print('Second')
        elif now==2:print('First')
        else:go(past[now-2],now-2,now,past[:-2],1)
    else:
        if now+1==limit:go(past[now-1],now-1,limit,past[:-1],1)
        else:
            x=branch[now].pop()
            s=s[:x]+s[x+1:]
            past+=(s,)
            go(s,now+1,limit,past,0)

def calc(s):
    data=[0 for i in range(len(s))]
    log = {}
    for i in range(len(s)):
        if s[i] in log:
            for j in range(log[s[i]]+1,i):
                data[j]+=1
            log[s[i]]=i
        elif s[i] not in log:
            log[s[i]]=i
    return data


def first(s,first_limit):
    first_branch=check(s)
    if first_branch==[]:
        if first_limit==1:return first_limit+1
        else: return first_limit
    data=calc(s)
    data_can=[]
    for i in first_branch:
        data_can.append(data[i])
    x=data_can.index(max(data_can))
    s=s[:x]+s[x+1:]
    first_limit+=1
    return first(s,first_limit)


first_limit=1
limit=first(s,first_limit)
branch={}
branch[1]=check(s)
past=(s,)
now=1
go(s,now,limit,past,0)