Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= -1):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)


Fixed submission:
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
T = []
D = []

ord_A = ord('A')

for i in range(Q):
  t,d = input().split()
  #T.append(ord(t) - ord_A)
  T.append(t)
  D.append(d)

#P = [[] for _ in range(ord('Z') - ord('A') + 1)]
#for i in range(N):
#  p = ord(S[i]) - ord_A
#  P[p].append(i)

G = []
for i in range(Q-1,-1,-1):
  t = T[i]
  d = D[i]
  
  
  for g in G:
    #print(g[0], g[1])
    if ('L' == d):
      tmp = g[0]+1
      if (tmp < N):
        if (t == S[tmp]):
        	G.append((tmp, S[tmp]))
    elif ('R' == d):
      tmp = g[0]-1
      if (0 < tmp):
        if (t == S[tmp]):
        	G.append((tmp, S[tmp]))
  
  if ('L' == d):
    if (t == S[0]):
      G.append((0, S[0]))
  elif ('R' == d):
    if (t == S[N-1]):
      G.append((N-1, S[N-1]))
  #print(G)
  G = list(set(G))
  

print(N - len(G))


Fixed submission:
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

ord_A = ord('A')

P = [[] for _ in range(ord('Z') - ord('A') + 1)]
for i in range(N):
  p = ord(S[i]) - ord_A
  P[p].append(i)

dG = [0 for _ in range(N)]
G = [1 for _ in range(N)]
for i in range(Q):
  t,d = input().split()
  ti = ord(t) - ord_A
  
  for j in P[ti]:
    g = G[j]
    G[j] = 0
    dG[j] = g
  for j in P[ti]:
    dg = dG[j]
    if ('L' == d):
      if (0 < j):
        G[j-1] += dg
    elif ('R' == d):
      if (j < (N-1)):
        G[j+1] += dg
print(sum(G))


Fixed submission:
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

P = [[] for _ in range(ord('Z') - ord('A') + 1)]
for i in range(N):
  p = ord(S[i]) - ord('A')
  P[p].append(i)

dG = [0 for _ in range(N)]
G = [1 for _ in range(N)]
for i in range(Q):
  t,d = input().split()
  ti = ord(t) - ord('A')
  
  for j in P[ti]:
    g = G[j]
    G[j] = 0
    dG[j] = g
  for j in P[ti]:
    dg = dG[j]
    if ('L' == d):
      if (0 < j):
        G[j-1] += dg
    elif ('R' == d):
      if (j < (N-1)):
        G[j+1] += dg
print(sum(G))


Fixed submission:
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

P = [[] for _ in range(ord('Z') - ord('A') + 1)]
for i in range(N):
  p = ord(S[i]) - ord('A')
  P[p].append(i)

G = [1 for _ in range(N)]
for i in range(Q):
  t,d = input().split()
  ti = ord(t) - ord('A')
  dG = []
  
  for j in P[ti]:
    g = G[j]
    G[j] = 0
    dG.append(g)
  for j in P[ti]:
    dg = dG.pop(0)
    if ('L' == d):
      if (0 < j):
        G[j-1] += dg
    elif ('R' == d):
      if (j < (N-1)):
        G[j+1] += dg
print(sum(G))


Fixed submission:
N,Q = map(int, input().split())
S = input()
C = [tuple(map(str, input().split())) for _ in range(Q)]
C = C[::-1]

left = -1
right = N

for (t,d) in C:
  if ('L' == d):
    if (S[left+1] == t):
      left += 1
      if (N-1 <= left):
        print(0)
        exit()
    if (right < N):
      if (S[right] == t):
        right += 1
        if (N < right):
          right = N
  else:
    if (S[right-1] == t):
      right -= 1
      if (right <= 0):
        print(0)
        exit()
    if (-1 < left):
      if (S[left] == t):
        left -= 1
        if (left < -1):
          left = -1

print(right - left - 1)