Buggy python submission (verdict: Time Limit Exceeded):
# import time
#
# start = time.time()

import sys
input = sys.stdin.readline
N,Q = [int(n) for n in input().split()]
S = "f" + str(input()) + "f"
# print(S)
t = [""]*Q
d = [""]*Q
for i in range(Q):
    t[i],d[i] = [str(n) for n in input().split()]

#左に落ちるやつで一番右のゴーレムのインデックスを探す
def leftbinarysearch():
    l = 0
    r = N + 1
    # cnt = 0#デバグ用カウンタ
    while(l<r):
        m = (l+r)//2 + 1#探索範囲の中央値のIndexのゴーレムでシミュレーション
        cur = m#探す一つのゴーレムの現在地
        fall = False
        # print(l,r,m)
        for i in range(Q):
            if t[i] == S[cur]:
                if d[i] == "L":
                    cur-=1
                else:
                    cur+=1
                if(cur == 0):#左端に落ちるなら次はもっと右のやつを確かめる
                    l = m
                    fall = True
                    break
                elif(cur == N+1):#右端に落ちるなら下の処理に飛ぶ
                    break
        if not fall:#落ちなかったならそれより左端を探す
            r = m - 1
        # cnt+=1
        # if(cnt == 10):print("loop", file=sys.stderr);break;
    return l

#→に落ちるやつを返す
def rightbinarysearch():
        l = 0
        r = N + 1
        # cnt = 0
        while(l<r):
            m = (l+r)//2#探索範囲の中央値のIndexのゴーレムでシミュレーション
            cur = m#探す一つのゴーレムの現在地
            fall = False
            # print(l,r,m)
            for i in range(Q):
                if t[i] == S[cur]:
                    if d[i] == "L":
                        cur-=1
                    else:
                        cur+=1
                    if(cur == N+1):#右端に落ちるなら次はもっと左のやつを確かめる
                        r = m
                        fall = True
                        break
                    elif(cur == -1):#左端に落ちるなら下の処理まで飛ぶ
                        break
            if not fall:#落ちなかったならそれより右端を探す
                l = m + 1
            # cnt+=1
            # if(cnt == 10):print("loop", file=sys.stderr);break;
        return l
# print(rightbinarysearch())
# print(leftbinarysearch())
print(rightbinarysearch()-leftbinarysearch()-1)

# end = time.time()
# print(end - start)


Fixed submission:
n, q = [int(_) for _ in input().split()]
s = input()

Q = [(input().split()) for i in range(q)]
Q.reverse()
l = 0
r = n - 1
for t,d in Q:
    if s[l] == t and d == "L":
        l += 1
    elif l > 0 and s[l - 1] == t and d == "R":
        l -= 1
    if s[r] == t and d == "R":
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == "L":
        r += 1
    # print(r,l,r-l+1)
    if l > r:
        break
print(r - l + 1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N,Q = [int(n) for n in input().split()]
S = "f" + str(input()) + "f"
# print(S)
t = [""]*Q
d = [""]*Q
for i in range(Q):
    t[i],d[i] = [str(n) for n in input().split()]

#左に落ちるやつで一番右のゴーレムのインデックスを探す
def leftbinarysearch():
    l = 0
    r = N + 1
    cnt = 0#デバグ用カウンタ
    while(l<r):
        m = (l+r)//2 + 1#探索範囲の中央値のIndexのゴーレムでシミュレーション
        cur = m#探す一つのゴーレムの現在地
        fall = False
        # print(l,r,m)
        for i in range(Q):
            if t[i] == S[cur]:
                if d[i] == "L":
                    cur-=1
                else:
                    cur+=1
                if(cur == 0):#左端に落ちるなら次はもっと右のやつを確かめる
                    l = m
                    fall = True
                    break
                elif(cur == N+1):#右端に落ちるなら下の処理に飛ぶ
                    break
        if not fall:#落ちなかったならそれより左端を探す
            r = m - 1
        cnt+=1
        # if(cnt == 10):print("loop", file=sys.stderr);break;
    return l

#→に落ちるやつを返す
def rightbinarysearch():
        l = 0
        r = N + 1
        cnt = 0
        while(l<r):
            m = (l+r)//2#探索範囲の中央値のIndexのゴーレムでシミュレーション
            cur = m#探す一つのゴーレムの現在地
            fall = False
            # print(l,r,m)
            for i in range(Q):
                if t[i] == S[cur]:
                    if d[i] == "L":
                        cur-=1
                    else:
                        cur+=1
                    if(cur == N+1):#右端に落ちるなら次はもっと左のやつを確かめる
                        r = m
                        fall = True
                        break
                    elif(cur == -1):#左端に落ちるなら下の処理まで飛ぶ
                        break
            if not fall:#落ちなかったならそれより右端を探す
                l = m + 1
            cnt+=1
            # if(cnt == 10):print("loop", file=sys.stderr);break;
        return l
# print(rightbinarysearch())
# print(leftbinarysearch())
print(rightbinarysearch()-leftbinarysearch()-1)


Fixed submission:
n, q = [int(_) for _ in input().split()]
s = input()

Q = [(input().split()) for i in range(q)]
Q.reverse()
l = 0
r = n - 1
for t,d in Q:
    if s[l] == t and d == "L":
        l += 1
    elif l > 0 and s[l - 1] == t and d == "R":
        l -= 1
    if s[r] == t and d == "R":
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == "L":
        r += 1
    # print(r,l,r-l+1)
    if l > r:
        break
print(r - l + 1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N,Q = [int(n) for n in input().split()]
S = "f" + str(input()) + "f"
# print(S)
t = [""]*Q
d = [""]*Q
for i in range(Q):
    t[i],d[i] = [str(n) for n in input().split()]

#左に落ちるやつで一番右のゴーレムのインデックスを探す
def leftbinarysearch():
    l = 0
    r = N + 1
    cnt = 0#デバグ用カウンタ
    while(l<r):
        m = (l+r)//2 + 1#探索範囲の中央値のIndexのゴーレムでシミュレーション
        cur = m#探す一つのゴーレムの現在地
        fall = False
        # print(l,r,m)
        for i in range(Q):
            if t[i] == S[cur]:
                if d[i] == "L":
                    cur-=1
                else:
                    cur+=1
            if(cur == 0):#左端に落ちるなら次はもっと右のやつを確かめる
                l = m
                fall = True
                break
            elif(cur == N+1):#右端に落ちるなら下の処理に飛ぶ
                break
        if not fall:#落ちなかったならそれより左端を探す
            r = m - 1
        cnt+=1
        # if(cnt == 10):print("loop", file=sys.stderr);break;
    return l

#→に落ちるやつを返す
def rightbinarysearch():
        l = 0
        r = N + 1
        cnt = 0
        while(l<r):
            m = (l+r)//2#探索範囲の中央値のIndexのゴーレムでシミュレーション
            cur = m#探す一つのゴーレムの現在地
            fall = False
            # print(l,r,m)
            for i in range(Q):
                if t[i] == S[cur]:
                    if d[i] == "L":
                        cur-=1
                    else:
                        cur+=1
                if(cur == N+1):#右端に落ちるなら次はもっと左のやつを確かめる
                    r = m
                    fall = True
                    break
                elif(cur == -1):#左端に落ちるなら下の処理まで飛ぶ
                    break
            if not fall:#落ちなかったならそれより右端を探す
                l = m + 1
            cnt+=1
            # if(cnt == 10):print("loop", file=sys.stderr);break;
        return l
# print(rightbinarysearch())
# print(leftbinarysearch())
print(rightbinarysearch()-leftbinarysearch()-1)


Fixed submission:
n, q = [int(_) for _ in input().split()]
s = input()

Q = [(input().split()) for i in range(q)]
Q.reverse()
l = 0
r = n - 1
for t,d in Q:
    if s[l] == t and d == "L":
        l += 1
    elif l > 0 and s[l - 1] == t and d == "R":
        l -= 1
    if s[r] == t and d == "R":
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == "L":
        r += 1
    # print(r,l,r-l+1)
    if l > r:
        break
print(r - l + 1)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import exit
N,Q = [int(n) for n in input().split()]
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
dic ={'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6,
 'H': 7, 'I': 8, 'J': 9, 'K': 10,'L': 11, 'M': 12, 'N': 13, 'O': 14,
  'P': 15, 'Q': 16, 'R': 17, 'S': 18,'T': 19, 'U': 20, 'V': 21,
   'W': 22, 'X': 23, 'Y': 24, 'Z': 25}
adj = [set() for _ in range(26)]
gorem = [1 for _ in range(N)]
goremap = dict(enumerate(gorem))
# print(goremap)
ap = set()
for i in range(L):
    adj[dic[S[i]]].add(i)
# print(adj)
INF = 10**9
ans = N

for i in range(Q):
    #t == 文字、D = {L,R}
    t,d = [str(n) for n in input().split()]
    if d == "L":
        # print(adj[dic[t]])
        for mass in adj[dic[t]]:#その支持のマス
            gon = gorem[mass]
            gorem[mass]-=gon
            if mass == 0 :
                ans-=gon
                # print("die")
            else:
                gorem[mass-1]+=gon
    else:
        # print(adj[dic[t]])
        for mass in adj[dic[t]]:#その支持のマス
            gon = gorem[mass]
            gorem[mass]-=gon
            if mass == L-1:
                ans-=gon
                # print("die")
            else:
                gorem[mass+1]+=gon
    # print(gorem)

# print(adj)
print(ans)
# T = str(input())
# exit()


Fixed submission:
n, q = [int(_) for _ in input().split()]
s = input()

Q = [(input().split()) for i in range(q)]
Q.reverse()
l = 0
r = n - 1
for t,d in Q:
    if s[l] == t and d == "L":
        l += 1
    elif l > 0 and s[l - 1] == t and d == "R":
        l -= 1
    if s[r] == t and d == "R":
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == "L":
        r += 1
    # print(r,l,r-l+1)
    if l > r:
        break
print(r - l + 1)