Buggy python submission (verdict: Time Limit Exceeded):
class grid:
    def __init__(self, word):
        self.word = word
        self.has_golem = 1
        self.will_move_golem = 0


def move_golem(text, dir):
    for i in range(n):
        if grids[i].word == text and grids[i].has_golem != 0:
            move_golem = grids[i].has_golem
            grids[i].has_golem = 0
            if(dir == 'L' and i != 0):
                grids[i-1].will_move_golem += move_golem
            if(dir == 'R' and i != n-1):
                grids[i+1].will_move_golem += move_golem
        else:
            continue
    for g in grids:
        g.has_golem += g.will_move_golem
        g.will_move_golem = 0

def answer():
    sum_golem = 0
    for g in grids:
        sum_golem += g.has_golem

    return sum_golem

n, q = map(int, input().split())
s = input()

grids = []
for i in range(n):
    grids.append(grid(s[i]))

for i in range(q):
    text, dir = input().split()
    move_golem(text, dir)

print(answer())


Fixed submission:
class spell:
    def __init__(self, text, dir):
        self.text = text
        self.dir = dir

def isRange(a):
    if(0 <= a < n):
        return True
    else:
        return False

n, q = map(int, input().split())
s = input()

spells = []
for i in range(q):
    text, dir = input().split()
    spells.append(spell(text, dir))


l = -1
r = n

for sp in reversed(spells):
    if(isRange(l+1) and s[l+1] == sp.text and sp.dir == 'L'):
        l += 1
    elif(isRange(l) and s[l] == sp.text and sp.dir == 'R'):
        l -= 1
    if(isRange(r-1) and s[r-1] == sp.text and sp.dir == 'R'):
        r -= 1
    elif(isRange(r) and s[r] == sp.text and sp.dir == 'L'):
        r += 1

print(n-(l+1)-(n-r))

Buggy python submission (verdict: Time Limit Exceeded):
class grid:
    def __init__(self, word):
        self.word = word
        self.has_golem = 1
        self.will_move_golem = 0

# def show_grid(grids):
#     print("----------------")
#     for g in grids:
#         print("| " + g.word + " ", end="")
#     print("|")

#     for g in grids:
#         print("| " + str(g.has_golem) + " ", end="")
#     print("|")
#     print("----------------")

def move_golem(text, dir, grids):
    for i in range(n):
        if grids[i].word == text and grids[i].has_golem != 0:
            move_golem = grids[i].has_golem
            grids[i].has_golem = 0
            if(dir == 'L' and i != 0):
                grids[i-1].will_move_golem += move_golem
            if(dir == 'R' and i != n-1):
                grids[i+1].will_move_golem += move_golem
        else:
            continue
    for g in grids:
        g.has_golem += g.will_move_golem
        g.will_move_golem = 0

def answer(grids):
    sum_golem = 0
    for g in grids:
        sum_golem += g.has_golem

    return sum_golem

n, q = map(int, input().split())
s = input()

grids = []
for i in range(n):
    grids.append(grid(s[i]))

# show_grid(grids)

for i in range(q):
    text, dir = input().split()
    # print(text + " " + dir)
    move_golem(text, dir, grids)
    # show_grid(grids)

print(answer(grids))


Fixed submission:
class spell:
    def __init__(self, text, dir):
        self.text = text
        self.dir = dir

def isRange(a):
    if(0 <= a < n):
        return True
    else:
        return False

n, q = map(int, input().split())
s = input()

spells = []
for i in range(q):
    text, dir = input().split()
    spells.append(spell(text, dir))


l = -1
r = n

for sp in reversed(spells):
    if(isRange(l+1) and s[l+1] == sp.text and sp.dir == 'L'):
        l += 1
    elif(isRange(l) and s[l] == sp.text and sp.dir == 'R'):
        l -= 1
    if(isRange(r-1) and s[r-1] == sp.text and sp.dir == 'R'):
        r -= 1
    elif(isRange(r) and s[r] == sp.text and sp.dir == 'L'):
        r += 1

print(n-(l+1)-(n-r))