Buggy python submission (verdict: Runtime Error):
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit < 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))


Fixed submission:
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit <= 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n-1:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))

Buggy python submission (verdict: Runtime Error):
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1

print(max(rightLimit - leftLimit - 1 , 0))


Fixed submission:
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit <= 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n-1:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))

Buggy python submission (verdict: Runtime Error):
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

letterPlace = {}

golemGroup = {}

for i, letter in enumerate(list(s)):
  if letter in letterPlace:
    letterPlace[letter].append(i)
  else:
    letterPlace[letter] = [i]
  golemGroup[i] = 1

#print(golemGroup)

move = {
  "R" : 1,
  "L" : -1
}

for _ in range(q):
  spell = sys.stdin.readline().split()
  if spell[0] not in letterPlace:
    continue
  places = letterPlace[spell[0]]
  nextGroup = copy.copy(golemGroup)
  for place in places:
    if place == 0 and spell[1] == "L":
      del(nextGroup[place])
    elif place == n-1 and spell[1] == "R":
      del(nextGroup[place])
    else:
      if place not in golemGroup:
        continue
      if place + move[spell[1]] in golemGroup:
        nextGroup[place + move[spell[1]]] += golemGroup[place]
        del(nextGroup[place])
      else:
        nextGroup[place + move[spell[1]]] = golemGroup[place]
        del(nextGroup[place])
    golemGroup = copy.copy(nextGroup)
    #print(golemGroup)

out = 0
for v in golemGroup.values():
  out += len(v)

print(out)

Fixed submission:
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit <= 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n-1:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))

Buggy python submission (verdict: Time Limit Exceeded):
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

letterPlace = {}

golemGroup = {}

for i, letter in enumerate(list(s)):
  if letter in letterPlace:
    letterPlace[letter].append(i)
  else:
    letterPlace[letter] = [i]
  golemGroup[i] = [1]

#print(golemGroup)

move = {
  "R" : 1,
  "L" : -1
}

for _ in range(q):
  spell = sys.stdin.readline().split()
  if spell[0] not in letterPlace:
    continue
  places = letterPlace[spell[0]]
  nextGroup = copy.copy(golemGroup)
  for place in places:
    if place == 0 and spell[1] == "L":
      del(nextGroup[place])
    elif place == n-1 and spell[1] == "R":
      del(nextGroup[place])
    else:
      if place not in golemGroup:
        continue
      if place + move[spell[1]] in golemGroup:
        nextGroup[place + move[spell[1]]] += golemGroup[place]
        del(nextGroup[place])
      else:
        nextGroup[place + move[spell[1]]] = golemGroup[place]
        del(nextGroup[place])
    golemGroup = copy.copy(nextGroup)
    #print(golemGroup)

out = 0
for v in golemGroup.values():
  out += len(v)

print(out)

Fixed submission:
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit <= 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n-1:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split(" "))

s = input()

letterPlace = {}

for i, letter in enumerate(list(s)):
  if letter in letterPlace:
    letterPlace[letter].append(i)
  else:
    letterPlace[letter] = [i]
    
# print(letterPlace)

golems = [1] * n

for _ in range(q):
  spell = input().split(" ")
  if spell[0] not in letterPlace:
    continue
    
  if spell[1] == "L":
    for place in letterPlace[spell[0]]:
      if place == 0:
        golems[0] = 0
      else:
        golems[place - 1] += golems[place]
        golems[place] = 0
  else:
    for place in reversed(letterPlace[spell[0]]):
      if place == n-1:
        golems[n-1] =0
      else:
        golems[place + 1] += golems[place]
        golems[place] = 0

print(sum(golems))

Fixed submission:
import copy
import sys

n, q = map(int, sys.stdin.readline().split())

s = input()

leftLimit = -1
rightLimit = n

spells =   spell = sys.stdin.readlines()
  
for spell in reversed(spells):
  target, direction = spell.split()
  if direction == "R" and target == s[rightLimit - 1]:
    rightLimit -= 1
    if rightLimit <= 0:
      break
  try:
    if direction == "R" and target == s[leftLimit]:
      if leftLimit != -1:
         leftLimit -= 1
  except Exception as e:
    pass
  if direction == "L" and target == s[leftLimit + 1]:
    leftLimit += 1
    if leftLimit >= n-1:
      break
  try:  
    if direction =="L" and target == s[rightLimit]:
      if rightLimit != n:
        rightLimit += 1
  except Exception as e:
    pass
  #print(target, direction, leftLimit, rightLimit)

print(max(rightLimit - leftLimit - 1 , 0))