Buggy python submission (verdict: Time Limit Exceeded):
nq = input().split()
n = int(nq[0])
q = int(nq[1])
block_word = [""]
block_num = [""]
block_move = [""]
golem = n
s = input()
t = ""
d = ""

for word in s:
  block_word += [word]
  block_move += [0]
  block_num += [1]

for i in range(q):
  td = input().split()
  t = td[0]
  d = td[1]
  for j in range(1, n+1):
    if t == block_word[j]:
      if j == n and d == "R":
        golem -= block_num[n]
        block_num[n] = 0
      elif j == 1 and d == "L":
        golem -= block_num[1]
        block_num[1] = 0
      else:
        if d == "R":
          block_move[j+1] = block_num[j]
          block_num[j] = 0
        if d == "L":
          block_move[j-1] = block_num[j]
          block_num[j] = 0
    else:
        j=j
  for k in range(1,n+1):
    block_num[k] += block_move[k]
  for i in range(1,n+1):
    block_move[i] = 0

print(golem)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[input().split() for i in range(Q)]
 
L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1
 
print(R-L-1)

Buggy python submission (verdict: Runtime Error):
nq=input().split()
n=int(nq[0])
q=int(nq[1])
block_word=[""]
block_num=[""]
block_move=[""]
golem=n
s=input()
t=""
d=""

for i in range(q):
  block_num += [1]

for word in s:
  block_word += [word]
  block_move+=[0]

for i in range(q):
  td=input().split()
  t=td[0]
  d=td[1]
  for j in range(1,n+1):
    if t==block_word[j]:
      if j==n and d=="R":
        golem-=block_num[n]
        block_num[n]=0
      elif j==1 and d=="L":
        golem-=block_num[1]
        block_num[1]=0
      else:
        if d=="R":
          block_move[j+1]=block_num[j]
          block_num[j]=0
        if d=="L":
          block_move[j-1]=block_num[j]
          block_num[j]=0
    else:
        block_move[j]=0
  for k in range(1,n+1):
    block_num[k]+=block_move[k]
  for i in range(1,n+1):
    block_move[i]=0

print(golem)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[input().split() for i in range(Q)]
 
L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1
 
print(R-L-1)

Buggy python submission (verdict: Runtime Error):
nq=input().split()
n=int(nq[0])
q=int(nq[1])
block_word=[""]
block_num=[""]
block_move=[""]
golem=n
s=input()
t=""
d=""

for i in range(q):
  block_num += [1]

for word in s:
  block_word += [word]
  block_move+=[0]

for i in range(q):
  print(golem)
  td=input().split()
  t=td[0]
  d=td[1]
  for j in range(1,n+1):
    if t==block_word[j]:
      if j==n and d=="R":
        golem-=block_num[n]
        block_num[n]=0
      elif j==1 and d=="L":
        golem-=block_num[1]
        block_num[1]=0
      else:
        if d=="R":
          block_move[j+1]=block_num[j]
          block_num[j]=0
        if d=="L":
          block_move[j-1]=block_num[j]
          block_num[j]=0
    else:
        block_move[j]=0
  for k in range(1,n+1):
    block_num[k]+=block_move[k]
  for i in range(1,n+1):
    block_move[i]=0

print(golem)

Fixed submission:
N,Q=map(int,input().split())
s="0"+input()+"0"
queri=[input().split() for i in range(Q)]
 
L,R=0,N+1
for i in range(Q-1,-1,-1):
    t,d=queri[i]
    if t==s[L] and d=="R":
        L-=1
    elif t==s[L+1] and d=="L":
        L+=1
    if t==s[R] and d=="L":
        R+=1
    elif t==s[R-1] and d=="R":
        R-=1
 
print(R-L-1)