Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = input().split()


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    #print(s1,sN)


print(sN-s1+1)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())

mas = [[-1] for i in range(26)]
for n in range(N):
    mas[ord(S[n])-65].append(n)

cnt = [1]*N

for q in range(Q):
    if d[q] == 'L':
        for n in mas[ord(t[q])-65]:
            if n == -1:
                continue
            if n == 0:
                cnt[n] = 0
            else:
                cnt[n-1] += cnt[n]
                cnt[n] = 0 
    else:
        for n in reversed(mas[ord(t[q])-65]):
            if n == -1:
                continue
            if n == N-1:
                cnt[n] = 0
            else:
                cnt[n+1] += cnt[n]
                cnt[n] = 0


print(sum(cnt))

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    #print(s1,sN)


print(sN-s1+1)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    #print(s1,sN)


print(sN-s1+1)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1]:
        if d[q] == 'L':
            s1 += 1
    if t[q] == S[sN]:
        if d[q] == 'R':
            sN -= 1
    #print(s1,sN)


print(sN-s1+1)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())

mas = [[-1] for i in range(26)]
for n in range(N):
    mas[ord(S[n])-65].append(n)

cnt = [1]*N

for q in range(Q):
    if d[q] == 'L':
        for n in mas[ord(t[q])-65]:
            if n == -1:
                continue
            if n == 0:
                cnt[n] = 0
            else:
                cnt[n-1] += cnt[n]
                cnt[n] = 0 
    else:
        for n in reversed(mas[ord(t[q])-65]):
            if n == -1:
                continue
            if n == N-1:
                cnt[n] = 0
            else:
                cnt[n+1] += cnt[n]
                cnt[n] = 0


print(sum(cnt))

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())

mas = [[-1] for i in range(26)]
for n in range(N):
    mas[ord(S[n])-65].append(n)

cnt = [1]*N

for q in range(Q):
    if d[q] == 'L':
        for n in mas[ord(t[q])-65]:
            if n == -1:
                continue
            if n == 0:
                cnt[n] = 0
            else:
                cnt[n-1] += cnt[n]
                cnt[n] = 0 
    else:
        for n in reversed(mas[ord(t[q])-65]):
            if n == -1:
                continue
            if n == N-1:
                cnt[n] = 0
            else:
                cnt[n+1] += cnt[n]
                cnt[n] = 0

ans = 0
for n in range(N):
    ans += cnt[n]
print(ans)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())

mas = [0]*N
for n in range(N):
    mas[n] = S[n]
cnt = [1]*N

for q in range(Q):
    if d[q] == 'L':
        for n in range(N):
            if mas[n] == t[q]:
                if n == 0:
                    cnt[n] = 0
                else:
                    cnt[n-1] += cnt[n]
                    cnt[n] = 0 
    else:
        for n in reversed(range(N)):
            if mas[n] == t[q]:
                if n == N-1:
                    cnt[n] = 0
                else:
                    cnt[n+1] += cnt[n]
                    cnt[n] = 0

ans = 0
for n in range(N):
    ans += cnt[n]
print(ans)

Fixed submission:
N,Q = map(int, input().split())
S = input()

t = [0]*Q
d = [0]*Q
for q in range(Q):
    t[q],d[q] = map(str, input().split())


s1 = 0
sN = N-1

for q in reversed(range(Q)):
    if t[q] == S[s1] and d[q] == 'L':
        s1 += 1
    elif s1 > 0:
        if d[q] == 'R' and S[s1-1] == t[q]:
            s1 -= 1
    if t[q] == S[sN] and d[q] == 'R':
        sN -= 1
    elif sN < N-1:
        if d[q] == 'L' and S[sN+1] == t[q]:
            sN += 1
    if s1 > sN:
        break
    #print(s1,sN)


print(sN-s1+1)