Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def vanish_golem(pos, n, s, spells):
    for t, d in spells:
        if pos < 0:
            return -1
        if pos >= n:
            return 1

        if s[pos] == t:
            pos += 1 if d == 'R' else -1

    if pos < 0:
        return -1
    elif pos >= n:
        return 1
    else:
        return 0


def search_vanish_golem(n, s, spells, th):
    l = 0
    h = n-1

    while l <= h:
        t = (l+h) // 2
        v = vanish_golem(t, n, s, spells)
        if v < th:
            l = t + 1
        elif v > th:
            h = t - 1

    return l


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

td = [input().split() for _ in range(q)]

l_cnt = search_vanish_golem(n, s, td, 0)
r_cnt = n - search_vanish_golem(n, s, td, 1)

print(n - l_cnt - r_cnt)


Fixed submission:
# -*- coding: utf-8 -*-
def vanish_golem(pos, n, s, spells):
    for t, d in spells:
        if pos < 0:
            return -1
        if pos >= n:
            return 1

        if s[pos] == t:
            pos += 1 if d == 'R' else -1

    if pos < 0:
        return -1
    elif pos >= n:
        return 1
    else:
        return 0


def search_vanish_golem(n, s, spells, th):
    l = 0
    h = n-1

    while l <= h:
        t = l + (h - l) // 2
        v = vanish_golem(t, n, s, spells)
        if v < th:
            l = t + 1
        else:
            h = t - 1

    return l


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

td = [input().split() for _ in range(q)]

l_cnt = search_vanish_golem(n, s, td, 0)
r_cnt = n - search_vanish_golem(n, s, td, 1)

print(n - l_cnt - r_cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, q = map(int, input().split())
s = input()

s_idx = {}
for i, c in enumerate(s):
    if c in s_idx:
        s_idx[c].append(i)
    else:
        s_idx[c] = [i]

golem_pos = [1 for _ in range(n)]
for _ in range(q):
    t, d = input().split()

    step = 1 if d == 'L' else -1

    if t not in s_idx:
        continue

    for i in s_idx[t][::step]:
        if i == 0 and d == 'L':
            golem_pos[i] = 0
        elif i == n-1 and d == 'R':
            golem_pos[i] = 0
        elif d == 'L':
            golem_pos[i-1] += golem_pos[i]
            golem_pos[i] = 0
        else:
            golem_pos[i+1] += golem_pos[i]
            golem_pos[i] = 0

print(sum(golem_pos))


Fixed submission:
# -*- coding: utf-8 -*-
def vanish_golem(pos, n, s, spells):
    for t, d in spells:
        if pos < 0:
            return -1
        if pos >= n:
            return 1

        if s[pos] == t:
            pos += 1 if d == 'R' else -1

    if pos < 0:
        return -1
    elif pos >= n:
        return 1
    else:
        return 0


def search_vanish_golem(n, s, spells, th):
    l = 0
    h = n-1

    while l <= h:
        t = l + (h - l) // 2
        v = vanish_golem(t, n, s, spells)
        if v < th:
            l = t + 1
        else:
            h = t - 1

    return l


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

td = [input().split() for _ in range(q)]

l_cnt = search_vanish_golem(n, s, td, 0)
r_cnt = n - search_vanish_golem(n, s, td, 1)

print(n - l_cnt - r_cnt)