Buggy python submission (verdict: Time Limit Exceeded):
N, Q = [int(x) for x in input().split()]
S = input()

SD = {}
for i in range(len(S)):
    a = SD.get(S[i], set())
    a.add(i)
    SD[S[i]] = a

G = [1] * N

D = {}
rmg = 0
for _ in range(Q):
    t, d = input().split()
    # print(G)

    if SD.get(t,0) == 0:
        continue

    if d == "L":
        for i in SD[t]:
            if i == 0:
                rmg += G[i]
                G[i] = 0
            else:
                G[i - 1] += G[i]
                G[i] = 0
    else:
        for i in SD[t]:
            if i == N - 1:
                rmg += G[i]
                G[i] = 0
            else:
                G[i + 1] += G[i]
                G[i] = 0

# print(G)

print(N - rmg)


Fixed submission:
N, Q = [int(x) for x in input().split()]
S = input()
TD = []
for _ in range(Q):
    t, d = input().split()
    TD.append((t, d))


def isDead(targetIdx):
    ti = targetIdx
    for t, d in TD:
        if t == S[ti]:
            if d == 'L':
                ti -= 1
                if ti == -1:
                    return -1
            else:
                ti += 1
                if ti == N:
                    return +1
    return 0


l, t, r = 0, N // 2, N - 1

while r - l > 1:
    t = (l + r) // 2
    if isDead(t) == -1:
        l = t
    else:
        r = t

L = l
if L == 0 and isDead(0) == 0:
    L -= 1

l, t, r = 0, N // 2, N - 1

while r - l > 1:
    t = (l + r) // 2
    if isDead(t) == +1:
        r = t
    else:
        l = t

R = r
if R == N - 1 and isDead(N - 1) == 0:
    R += 1

print(R - L - 1)

# print((L, R))
# print([isDead(t) for t in range(N)])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = [int(x) for x in input().split()]
S = input()

SD = {}
for i in range(len(S)):
    a = SD.get(S[i], set())
    a.add(i)
    SD[S[i]] = a

G = [1] * N

D = {}
rmg = 0
for _ in range(Q):
    t, d = input().split()
    # print(G)

    if SD.get(t,0) == 0:
        continue

    if d == "L":
        for i in SD[t]:
            if i == 0:
                rmg += G[i]
                G[i] = 0
            else:
                G[i - 1] += G[i]
                G[i] = 0
    else:
        for i in SD[t]:
            if i == N - 1:
                rmg += G[i]
                G[i] = 0
            else:
                G[i + 1] += G[i]
                G[i] = 0

# print(G)

print(N - rmg)


Fixed submission:
N, Q = [int(x) for x in input().split()]
S = input()
TD = []
for _ in range(Q):
    t, d = input().split()
    TD.append((t, d))


def isDead(targetIdx):
    ti = targetIdx
    for t, d in TD:
        if t == S[ti]:
            if d == 'L':
                ti -= 1
                if ti == -1:
                    return -1
            else:
                ti += 1
                if ti == N:
                    return +1
    return 0


l, t, r = 0, N // 2, N - 1

while r - l > 1:
    t = (l + r) // 2
    if isDead(t) == -1:
        l = t
    else:
        r = t

L = l
if L == 0 and isDead(0) == 0:
    L -= 1

l, t, r = 0, N // 2, N - 1

while r - l > 1:
    t = (l + r) // 2
    if isDead(t) == +1:
        r = t
    else:
        l = t

R = r
if R == N - 1 and isDead(N - 1) == 0:
    R += 1

print(R - L - 1)

# print((L, R))
# print([isDead(t) for t in range(N)])