Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N,Q=[int(x) for x in input().split()]
S=input()
A=defaultdict(list)
B=[1 for x in range(N)]
for i in range(N):
    A[S[i]].append(i)
killed=0
for q in range(Q):
    a,b=input().split()
    tmp=[]
    if b=="R":
        d=1
    else:
        d=-1
    for k in A[a]:
        if (k==0 and d==-1) or (k==N-1 and d==1):
            killed+=B[k]
        elif B[k]!=0:
            tmp.append([k,B[k],d])
        B[k]=0
    for t in tmp:
        B[t[0]+t[2]]+=t[1]
print(N-killed)

Fixed submission:
from collections import defaultdict

def testkilled(p,S,qry,N):
    pi=p
    c=S[pi]
    for q in qry:
        a,b=q
        if b=="R":
            d=1
        else:
            d=-1
        if c==a:
            pi+=d
            if pi==-1:
                return 1
            if pi==N:
                return 2
            c=S[pi]
    return 0

N,Q=[int(x) for x in input().split()]
S=input()
B=[1 for x in range(N)]
qry=[]
for q in range(Q):
    qry.append(input().split())
l=0
r=N
ans=N
while(r-l>1):
    p=(l+r)//2
    k=testkilled(p,S,qry,N)
    if k==1:
        l=p
    else:
        r=p
if testkilled(0,S,qry,N)==1:
    if testkilled(p,S,qry,N)==1:
        ans-= p+1
    else:
        ans-= p
    #print ("p",p,l,r)    
l=-1
r=N-1
while(r-l>1):
    p=(l+r)//2
    k=testkilled(p,S,qry,N)
    if k==2:
        r=p
    else:
        l=p
if testkilled(N-1,S,qry,N)==2:
    if testkilled(p,S,qry,N)==2:
        ans-= (N-p)
    else:
        ans-= (N-p-1)
#print ("p",p,l,r)
print(ans)
#print(testkilled(0,S,qry,N))
#print(testkilled(1,S,qry,N))
#print(testkilled(N-1,S,qry,N))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N,Q=[int(x) for x in input().split()]
S=input()
A=defaultdict(list)
B=[1 for x in range(N)]
for i in range(N):
    A[S[i]].append(i)
killed=0
for q in range(Q):
    a,b=input().split()
    tmp=[]
    if b=="R":
        d=1
    else:
        d=-1
    for k in A[a]:
        if (k==0 and d==-1) or (k==N-1 and d==1):
            killed+=B[k]
        else:
            tmp.append([k,B[k],d])
        B[k]=0
    for t in tmp:
        B[t[0]+t[2]]+=t[1]
print(N-killed)

Fixed submission:
from collections import defaultdict

def testkilled(p,S,qry,N):
    pi=p
    c=S[pi]
    for q in qry:
        a,b=q
        if b=="R":
            d=1
        else:
            d=-1
        if c==a:
            pi+=d
            if pi==-1:
                return 1
            if pi==N:
                return 2
            c=S[pi]
    return 0

N,Q=[int(x) for x in input().split()]
S=input()
B=[1 for x in range(N)]
qry=[]
for q in range(Q):
    qry.append(input().split())
l=0
r=N
ans=N
while(r-l>1):
    p=(l+r)//2
    k=testkilled(p,S,qry,N)
    if k==1:
        l=p
    else:
        r=p
if testkilled(0,S,qry,N)==1:
    if testkilled(p,S,qry,N)==1:
        ans-= p+1
    else:
        ans-= p
    #print ("p",p,l,r)    
l=-1
r=N-1
while(r-l>1):
    p=(l+r)//2
    k=testkilled(p,S,qry,N)
    if k==2:
        r=p
    else:
        l=p
if testkilled(N-1,S,qry,N)==2:
    if testkilled(p,S,qry,N)==2:
        ans-= (N-p)
    else:
        ans-= (N-p-1)
#print ("p",p,l,r)
print(ans)
#print(testkilled(0,S,qry,N))
#print(testkilled(1,S,qry,N))
#print(testkilled(N-1,S,qry,N))