Problem p03081 (python)

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 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))

Buggy 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))

Buggy 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)