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

def solve():
    N,Q=map(int,input().split())
    s=list(map(lambda e:ord(e)-65,list(input())))
    s2=[[] for i in range(26)]
    for i,c in enumerate(s):
        s2[c].append(i+1)
    r2=copy.copy(s2)
    for i in range(26):
        r2[i]=list(reversed(r2[i]))

    m=[1 for i in range(N+2)]
    m[0]=0
    m[N+1]=0

    #print()
    #print(s2)
    #print(r2)
    #print(0,0,s)
    #print(0,0,m)
    for i in range(Q,0,-1):
        t,d=sys.stdin.readline().rstrip().split()
        t=ord(t)-65
        if d=='L':
            for j in s2[t]:
                if m[j] != 0:
                    m[j-1]+=m[j]
                    m[j]=0
            #if i & 0xff == 0:
            #    s2[t]=[e for e in s2[t] if e <= i or e >= N-i]
        else:
            for j in r2[t]:
                if m[j] != 0:
                    m[j+1]+=m[j]
                    m[j]=0
            #if i & 0xff == 0:
            #    r2[t]=[e for e in r2[t] if e <= i or e >= N-i]
        #print(t,d,m)
    return N-m[0]-m[N+1]

print(solve())


Fixed submission:
import sys

N,Q=map(int,input().split())
s=[-1]
s+=list(map(lambda e:ord(e)-65,list(input())))
s+=[-1]
t=[]
d=[]
for i in range(Q):
    ti,di=sys.stdin.readline().rstrip().split()
    ti=ord(ti)-65
    di = -1 if di=='L' else +1
    t.append(ti)
    d.append(di)

def solve(n):
    for i in range(Q):
        if s[n]==t[i]:
            n+=d[i]
    return n

def find_match(x):
    f1 = 1
    f2 = N + 1
    while f1 != f2:
        f3 = (f1 + f2) // 2
        if solve(f3) == x:
            f1 = f3 + 1
        else:
            f2 = f3
    return f1

def find_notmatch(x):
    f1 = 1
    f2 = N + 1
    while f1 != f2:
        f3 = (f1 + f2) // 2
        #print(f1,f2,f3,solve(f3),x)
        if solve(f3) != x:
            f1 = f3 + 1
        else:
            f2 = f3
    return f1

#for i in range(1,N+1):
#    print(solve(i))

print(find_notmatch(N+1)-find_match(0))

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

def solve():
    N,Q=map(int,input().split())
    s=list(input())
    s2={}
    for i in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        s2[i]=[]
    for i,c in enumerate(s):
        s2[c].append(i+1)
    r2=copy.copy(s2)
    for k in r2.keys():
        r2[k]=list(reversed(r2[k]))

    m=[1 for i in range(N+2)]
    m[0]=0
    m[N+1]=0

    #print()
    #print(s2)
    #print(r2)
    #print(0,0,s)
    #print(0,0,m)
    for i in range(Q):
        t,d=sys.stdin.readline().rstrip().split()
        if d=='L':
            for i in s2[t]:
                m[i-1]+=m[i]
                m[i]=0
        else:
            for i in r2[t]:
                m[i+1]+=m[i]
                m[i]=0
        #print(t,d,m)
    return N-m[0]-m[N+1]

print(solve())


Fixed submission:
import sys

N,Q=map(int,input().split())
s=[-1]
s+=list(map(lambda e:ord(e)-65,list(input())))
s+=[-1]
t=[]
d=[]
for i in range(Q):
    ti,di=sys.stdin.readline().rstrip().split()
    ti=ord(ti)-65
    di = -1 if di=='L' else +1
    t.append(ti)
    d.append(di)

def solve(n):
    for i in range(Q):
        if s[n]==t[i]:
            n+=d[i]
    return n

def find_match(x):
    f1 = 1
    f2 = N + 1
    while f1 != f2:
        f3 = (f1 + f2) // 2
        if solve(f3) == x:
            f1 = f3 + 1
        else:
            f2 = f3
    return f1

def find_notmatch(x):
    f1 = 1
    f2 = N + 1
    while f1 != f2:
        f3 = (f1 + f2) // 2
        #print(f1,f2,f3,solve(f3),x)
        if solve(f3) != x:
            f1 = f3 + 1
        else:
            f2 = f3
    return f1

#for i in range(1,N+1):
#    print(solve(i))

print(find_notmatch(N+1)-find_match(0))