Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

dp = [set() for _ in range(Q)]
for i in range(Q):
  t, d = q[i]
  if t == s[0] and d == 'L':
    dp[i].add(0)
  elif t == s[N-1] and d == 'R':
    dp[i].add(N-1)
  
  if i == 0:
    continue

  for p in dp[i-1]:
    dp[i].add(p)
    if p-1 >= 0 and t == s[p-1] and d == 'R':
      dp[i].add(p-1)
    elif p+1 <= N-1 and t == s[p+1] and d == 'L':
      dp[i].add(p+1)

print(N-len(dp[Q-1]))

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

l = 0
r = N-1
for i in range(Q):
  t, d = q[i]
  if l-1 >= 0 and t == s[l-1] and d == 'R':
    l -= 1
  elif t == s[l] and d == 'L':
    l += 1

  if r+1 <= N-1 and t == s[r+1] and d == 'L':
    r += 1
  elif t == s[r] and d == 'R':
    r -= 1

print(max(0, r-l+1))

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

dp = [[] for _ in range(N)]
for i in range(N):
  t, d = q[i]
  if t == s[0] and d == 'L':
    dp[i].append(0)
  elif t == s[N-1] and d == 'R':
    dp[i].append(N-1)
  
  if i == 0:
    continue

  for p in dp[i-1]:
    if p-1 >= 0 and t == s[p-1] and d == 'R':
      dp[i].append(p-1)
    elif p+1 <= N-1 and t == s[p+1] and d == 'L':
      dp[i].append(p+1)

print(N-len(dp[N-1]))

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

l = 0
r = N-1
for i in range(Q):
  t, d = q[i]
  if l-1 >= 0 and t == s[l-1] and d == 'R':
    l -= 1
  elif t == s[l] and d == 'L':
    l += 1

  if r+1 <= N-1 and t == s[r+1] and d == 'L':
    r += 1
  elif t == s[r] and d == 'R':
    r -= 1

print(max(0, r-l+1))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
pos = defaultdict(list)
for i in range(N):
  x = s[i]
  pos[x].append(i)

golem = [1]*N
for _ in range(Q):
  t, d = input().split()
  if d == 'R':
    for i in pos[t]:
      x = golem[i]
      golem[i] = 0
      if i+1 <= N-1:
        golem[i+1] += x
  elif d == 'L':
    for i in pos[t]:
      if golem[i] == 0:
        continue
      x = golem[i]
      golem[i] = 0
      if i-1 >= 0:
        golem[i-1] += x
print(sum(golem))

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()
q = []
for _ in range(Q):
  q.append(input().split())
q.reverse()

l = 0
r = N-1
for i in range(Q):
  t, d = q[i]
  if l-1 >= 0 and t == s[l-1] and d == 'R':
    l -= 1
  elif t == s[l] and d == 'L':
    l += 1

  if r+1 <= N-1 and t == s[r+1] and d == 'L':
    r += 1
  elif t == s[r] and d == 'R':
    r -= 1

print(max(0, r-l+1))