Problem p03081 (python)

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

sN = []
sQ = []
dQ = []
Q = 0
N = 0


def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]:
            if dQ[i] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append(t)
        dQ.append(d)
        #sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from numba import jit

sN = []
sQ = []
Q = 0
N = 0


@jit
def gor(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'R':
                pos = pos + 1
            else:
                pos = pos - 1

            if pos < 0:
                return False
            if pos > N - 1:
                return True

    return False


@jit
def getr():
    l = 0
    r = N - 1
    res = N
    while r >= l:
        mid = (l + r) // 2
        if gor(mid):
            res = mid
            r = mid - 1
        else:
            l = mid + 1
    return res


@jit
def gol(pos):
    for i in range(Q):
        if sN[pos] == sQ[i]['t']:
            if sQ[i]['d'] == 'L':
                pos = pos - 1
            else:
                pos = pos + 1

            if pos > N - 1:
                return False
            if pos < 0:
                return True

    return False


@jit
def getl():
    l = 0
    r = N - 1
    res = -1
    while l <= r:
        mid = (l + r) // 2
        if gol(mid):
            res = mid
            l = mid + 1
        else:
            r = mid - 1
    return res


if __name__ == '__main__':
    N, Q = map(int, input().split())  # N, Q for number

    sN = input()  # squares

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    removed_l = getl() + 1
    removed_r = N - getr()

    print(N - removed_l - removed_r)

Buggy submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    # init char list
    charList = [chr(s) for s in range(65, 91)] # A-Z

    N, Q = map(int, input().split()) # N, Q for number

    sN = list(input())  # squares
    sList = [None] * 26
    gList = [i for i in range(0,N)]  # golems
    for i in range(0, len(sN)): # init
        if(sList[charList.index(sN[i])] == None):
            sList[charList.index(sN[i])] = [{'id':i, 'loc':i}]
        else:
            sList[charList.index(sN[i])].append({'id':i, 'loc':i})

    sQ = []

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    remain = len(sN)
    for q in sQ: # loop to handle Q
        for golem in sList[charList.index(q['t'])]:
            if(q['d'] == 'L'):
                if(golem['loc'] == 0):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif(golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] - 1
                    sList[charList.index(sN[golem['loc']])].append({'id': golem['id'],'loc': golem['loc']})
            elif(q['d'] == 'R'):
                if(golem['loc'] == remain - 1):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif (golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] + 1
                    sList[charList.index(sN[golem['loc']])].append({'id': golem['id'],'loc': golem['loc']})

    print(remain)

Buggy submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    # init char list
    charList = [chr(s) for s in range(65, 91)] # A-Z

    N, Q = map(int, input().split()) # N, Q for number

    sN = list(input())  # squares
    sList = [None] * 26
    gList = [i for i in range(0,N)]  # golems
    for i in range(0, len(sN)): # init
        if(sList[charList.index(sN[i])] == None):
            sList[charList.index(sN[i])] = [{'id':i, 'loc':i}]
        else:
            sList[charList.index(sN[i])].append({'id':i, 'loc':i})

    sQ = []

    for i in range(0, Q):
        t, d = input().split()
        sQ.append({'t': t, 'd': d})

    remain = len(sN)
    for q in sQ: # loop to handle Q
        for golem in sList[charList.index(q['t'])]:
            if(q['d'] == 'L'):
                if(golem['loc'] == 0):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif(golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] - 1
                    sList[sN.index(golem['loc'])].append({'id': golem['id'],'loc': golem['loc']})
            elif(q['d'] == 'R'):
                if(golem['loc'] == remain - 1):
                    remain = remain - 1
                    golem['loc'] = -1  # drop
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': -1})
                elif (golem['loc'] > 0):
                    sList[sN.index(q['t'])].remove({'id': golem['id'],'loc': golem['loc']})
                    golem['loc'] = golem['loc'] + 1
                    sList[sN.index(golem['loc'])].append({'id': golem['id'],'loc': golem['loc']})

    print(remain)