Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
squares = np.zeros(N+2, np.bool)
squares[0] = 1; squares[N+1] = 1
S = np.array(list('-' + sr() + '-'))
TD = [sr().split() for _ in range(Q)]
start = ord('A')
alpha_index = dict()
for x in range(start, start+26):
    y = chr(x)
    alpha_index[y] = np.where(S==y)[0]
for t, d in TD[::-1]:
    index = alpha_index[t]
    if d == 'L':
        i = -1
    else:
        i = 1
    squares[index] = squares[index] | squares[index+i]

answer = N + 2 - np.count_nonzero(squares)
print(answer)
# 49


Fixed submission:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
S = '-' + sr() + '-'
TD = [sr().split() for _ in range(Q)]

L = 1
for t, d in TD[::-1]:
    if d == 'L':
        if L <= N and S[L] == t:
            L += 1
    if d == 'R':
        if L >= 1 and S[L-1] == t:
            L -= 1
R = N
for t, d in TD[::-1]:
    if d == 'L':
        if R <= N and S[R+1] == t:
            R += 1
    if d == 'R':
        if R >= 1 and S[R] == t:
            R -= 1

answer = max(0, R - L + 1)
print(answer)
# 34

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
squares = np.zeros(N+2, np.bool)
squares[0] = 1; squares[N+1] = 1
S = np.array(list('-' + sr() + '-'))
TD = [sr().split() for _ in range(Q)]
for t, d in TD[::-1]:
    index = np.where(S==t)[0]
    if d == 'L':
        i = -1
    else:
        i = 1
    squares[index] = squares[index] + squares[index+i]

answer = N + 2 - np.count_nonzero(squares)
print(answer)
# 49


Fixed submission:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
S = '-' + sr() + '-'
TD = [sr().split() for _ in range(Q)]

L = 1
for t, d in TD[::-1]:
    if d == 'L':
        if L <= N and S[L] == t:
            L += 1
    if d == 'R':
        if L >= 1 and S[L-1] == t:
            L -= 1
R = N
for t, d in TD[::-1]:
    if d == 'L':
        if R <= N and S[R+1] == t:
            R += 1
    if d == 'R':
        if R >= 1 and S[R] == t:
            R -= 1

answer = max(0, R - L + 1)
print(answer)
# 34

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
squares = np.zeros(N+2, np.bool)
squares[0] = 1; squares[N+1] = 1
S = np.array(list('-' + sr() + '-'))
TD = [sr().split() for _ in range(Q)]
for t, d in TD[::-1]:
    index = np.where(S==t)[0]
    if d == 'L':
        i = -1
    else:
        i = 1
    squares[index] = squares[index] | squares[index+i]

answer = N + 2 - np.count_nonzero(squares)
print(answer)
# 49


Fixed submission:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, Q = lr()
S = '-' + sr() + '-'
TD = [sr().split() for _ in range(Q)]

L = 1
for t, d in TD[::-1]:
    if d == 'L':
        if L <= N and S[L] == t:
            L += 1
    if d == 'R':
        if L >= 1 and S[L-1] == t:
            L -= 1
R = N
for t, d in TD[::-1]:
    if d == 'L':
        if R <= N and S[R+1] == t:
            R += 1
    if d == 'R':
        if R >= 1 and S[R] == t:
            R -= 1

answer = max(0, R - L + 1)
print(answer)
# 34