Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = input()
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R]:
				gake_R -= 1
		
		if d == "L":
			if t == S[gake_L]:
				gake_L += 1
	
	print(max(0, gake_R - gake_L+1))
	
	
if __name__ == "__main__":
	main()


Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	gorlm_num = [1] * len(S)
	
	skill = []
	for _ in range(Q):
		t, d = input().split(" ")
		#print(gorlm_num)
		ttmp = S.count(t)
		ttmp_sub = 0
		for _ in range(ttmp):
			try :
				tmpS = S[ttmp_sub+1:]
				ins = tmpS.index(t)
				ttmp_sub += ins
			except : a = "a"
			else : a = "a"
				
			#print(ins)
				
			tt = gorlm_num[ins]
			gorlm_num[ins] = 0
			if d == "R" and ins != len(S)-1:
				gorlm_num[ins+1] += tt
			if d == "L" and ins != 0:
				gorlm_num[ins-1] += tt
				
	ans = 0
	for i in gorlm_num:
		ans = ans + i
		
	print(ans)
	
if __name__ == "__main__":
	main()
	

Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	gorlm_num = [1] * len(S)
	
	skill = []
	for _ in range(Q):
		t, d = input().split(" ")
		#print(gorlm_num)
		tmp = [i for i, x in enumerate(S) if x == t]
		for i in tmp:
			tt = gorlm_num[i]
			gorlm_num[i] = 0
			if d == "R" and i != len(S)-1:
				gorlm_num[i+1] += tt
			if d == "L" and i != 0:
				gorlm_num[i-1] += tt
				
	ans = 0
	for i in gorlm_num:
		ans = ans + i
		
	print(ans)
	
if __name__ == "__main__":
	main()
	

Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	
	skill = []
	for _ in range(Q):
		t, d = input().split(" ")
		skill.append((t, d))
	
	gorlm_num = [1] * len(S)
	
	for i, val in enumerate(skill):
		t, d = val
		#print(gorlm_num)
		for j, vol in enumerate(S):
			if t == vol:
				tt = gorlm_num[j]
				gorlm_num[j] = 0
				if d == "R" and j != len(S)-1:
					gorlm_num[j+1] += tt
				if d == "L" and j != 0:
					gorlm_num[j-1] += tt
						
	ans = 0
	for i in gorlm_num:
		ans = ans + i
		
	print(ans)
	
if __name__ == "__main__":
	main()
	

Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	
	skill = []
	for _ in range(Q):
		t, d = input().split(" ")
		skill.append((t, d))
	
	gorlm_num = [1] * len(S)
	
	for i, val in enumerate(skill):
		t, d = val
		#print(gorlm_num)
		for j, vol in enumerate(S):
			if t == vol:
				tt = gorlm_num[j]
				gorlm_num[j] = 0
				if d == "R" and j != len(S)-1:
					gorlm_num[j+1] += tt
				if d == "L" and j != 0:
					if j != 0:
					gorlm_num[j-1] += tt
						
	ans = 0
	for i in gorlm_num:
		ans = ans + i
		
	print(ans)
	
if __name__ == "__main__":
	main()
	

Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	
	skill = []
	for _ in range(Q):
		t, d = input().split(" ")
		skill.append((t, d))
	
	gorlm_num = [1] * len(S)
	
	for i, val in enumerate(skill):
		t, d = val
		#print(gorlm_num)
		for j, vol in enumerate(S):
			if t == vol:
				if d == "R" and gorlm_num[j] > 0:
					tt = gorlm_num[j]
					gorlm_num[j] = 0
					if j != len(S)-1:
						gorlm_num[j+1] += tt
				if d == "L" and gorlm_num[j] > 0:
					tt = gorlm_num[j]
					gorlm_num[j] = 0
					if j != 0:
						gorlm_num[j-1] += tt
						
	ans = 0
	for i in gorlm_num:
		ans = ans + i
		
	print(ans)
	
if __name__ == "__main__":
	main()
	

Fixed submission:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N, Q = map(int, input().split())
	S = "_" + input() + "_"
	
	TD = []
	for _ in range(Q):
		tmp = input().split(" ")
		t = tmp[0]
		d = tmp[1]
		TD.append((t, d))
		
	TD.reverse()
	gake_L = 0
	gake_R = N - 1 + 2
	
	for i, val in enumerate(TD):
		t, d = val
		
		if d == "R":
			if t == S[gake_R-1]:
				gake_R -= 1
			if t == S[gake_L]:
				gake_L -= 1
		
		if d == "L":
			if t == S[gake_R]:
				gake_R += 1
			if t == S[gake_L+1]:
				gake_L += 1
	
	print(max(0, gake_R-gake_L-1))
	
	
if __name__ == "__main__":
	main()