Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000000)
input=lambda : sys.stdin.readline().rstrip('\n')

n,q=map(int,input().split())
s=input()
dic=[[0 for i in range(q)] for i in range(26)]
for i in range(q):
    t,d=input().split()
    c=ord(t)-ord('A')
    if d=='L':
        dic[c][i]=-1
    if d=='R':
        dic[c][i]=1

l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==-1:
            f=True
            break
        if d==n or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        l=mid
    else:
        r=mid
tmp1=l
l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==n:
            f=True
            break
        if d==-1 or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        r=mid
    else:
        l=mid
print(max(r-tmp1-1,0))

Fixed submission:
import sys
sys.setrecursionlimit(2000000000)
input=lambda : sys.stdin.readline().rstrip('\n')

n,q=map(int,input().split())
s=input()
dic=[[0 for i in range(q)] for i in range(26)]
for i in range(q):
    t,d=input().split()
    c=ord(t)-ord('A')
    if d=='L':
        dic[c][i]=-1
    if d=='R':
        dic[c][i]=1

l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==-1:
            f=True
            break
        if d==n or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        l=mid
    else:
        r=mid
tmp1=l
l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==n:
            f=True
            break
        if d==-1 or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        r=mid
    else:
        l=mid
print(max(r-tmp1-1,0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000000)
input=lambda : sys.stdin.readline().rstrip('\n')

n,q=map(int,input().split())
s=input()
dic=[[0 for i in range(q+1)] for i in range(26)]
for i in range(q):
    t,d=input().split()
    c=ord(t)-ord('A')
    if d=='L':
        dic[c][i]=-1
    if d=='R':
        dic[c][i]=1

def solve1(d,t):
    if d==-1:
        return True
    if d==n:
        return False
    while t<q+1 and dic[ord(s[d])-ord('A')][t]==0:
        t+=1
    if t==q+1:
        return False
    return solve1(d+dic[ord(s[d])-ord('A')][t],t+1)

def solve2(d,t):
    if d==n:
        return True
    if d==-1:
        return False
    while t<q+1 and dic[ord(s[d])-ord('A')][t]==0:
        t+=1
    if t==q+1:
        return False
    return solve2(d+dic[ord(s[d])-ord('A')][t],t+1)

l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    if solve1(mid,0):
        l=mid
    else:
        r=mid
tmp1=l
l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    if solve2(mid,0):
        r=mid
    else:
        l=mid
print(max(r-tmp1-1,0))

Fixed submission:
import sys
sys.setrecursionlimit(2000000000)
input=lambda : sys.stdin.readline().rstrip('\n')

n,q=map(int,input().split())
s=input()
dic=[[0 for i in range(q)] for i in range(26)]
for i in range(q):
    t,d=input().split()
    c=ord(t)-ord('A')
    if d=='L':
        dic[c][i]=-1
    if d=='R':
        dic[c][i]=1

l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==-1:
            f=True
            break
        if d==n or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        l=mid
    else:
        r=mid
tmp1=l
l,r=-1,n
while l+1<r:
    mid=(l+r)//2
    d=mid
    f=False
    for i in range(q+1):
        if d==n:
            f=True
            break
        if d==-1 or i==q:
            f=False
            break
        c=ord(s[d])-ord('A')
        if dic[c][i]!=0:
            d+=dic[c][i]
    if f:
        r=mid
    else:
        l=mid
print(max(r-tmp1-1,0))