Buggy python submission (verdict: Runtime Error):
def can_reach_to_0(s, comm, pos):
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos < 0:
            return True
    return False

def can_reach_to_N(s, comm, pos):
    N = len(s)
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos >= N:
            return True
    return False

N, Q = list(map(int, input().split()))
s = input()
comm = [input().split() for _ in range(Q)]

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_0(s, comm, mid):
        l = mid + 1
    else:
        r = mid - 1
left = r # いける右端index

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_N(s, comm, mid):
        r = mid - 1
    else:
        l = mid + 1
right = l # いける左端index
print(right-left-1)


Fixed submission:
def can_reach_to_0(s, comm, pos):
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos < 0:
            return True
        if pos >= N:
            return False
    return False

def can_reach_to_N(s, comm, pos):
    N = len(s)
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos >= N:
            return True
        if pos < 0:
            return False
    return False

N, Q = list(map(int, input().split()))
s = input()
comm = [input().split() for _ in range(Q)]

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_0(s, comm, mid):
        l = mid + 1
    else:
        r = mid - 1
left = r # いける右端index

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_N(s, comm, mid):
        r = mid - 1
    else:
        l = mid + 1
right = l # いける左端index
print(right-left-1)

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

N, Q = list(map(int, input().split()))
s = input()
dic = defaultdict(list)
for i, c in enumerate(s):
    dic[c].append(i)
golem = [1] * N
for _ in range(Q):
    t, d = input().split()
    if d == 'L':
        for i in dic[t]:
            if i > 0:
                golem[i-1] += golem[i]
                golem[i] = 0
            else:
                golem[i] = 0
    else:
        for i in dic[t]:
            if i < N-1:
                golem[i+1] += golem[i]
                golem[i] = 0
            else:
                golem[i] = 0
print(sum(golem))


Fixed submission:
def can_reach_to_0(s, comm, pos):
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos < 0:
            return True
        if pos >= N:
            return False
    return False

def can_reach_to_N(s, comm, pos):
    N = len(s)
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos >= N:
            return True
        if pos < 0:
            return False
    return False

N, Q = list(map(int, input().split()))
s = input()
comm = [input().split() for _ in range(Q)]

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_0(s, comm, mid):
        l = mid + 1
    else:
        r = mid - 1
left = r # いける右端index

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_N(s, comm, mid):
        r = mid - 1
    else:
        l = mid + 1
right = l # いける左端index
print(right-left-1)

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = list(map(int, input().split()))
s = input()
golem = [1] * N
for _ in range(Q):
    t, d = input().split()
    if d == 'L':
        for i, c in enumerate(s):
            if c == t:
                if i > 0:
                    golem[i-1] += golem[i]
                    golem[i] = 0
                else:
                    golem[i] = 0
    else:
        for i, c in enumerate(s):
            if c == t:
                if i < N-1:
                    golem[i+1] += golem[i]
                    golem[i] = 0
                else:
                    golem[i] = 0
print(sum(golem))


Fixed submission:
def can_reach_to_0(s, comm, pos):
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos < 0:
            return True
        if pos >= N:
            return False
    return False

def can_reach_to_N(s, comm, pos):
    N = len(s)
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos >= N:
            return True
        if pos < 0:
            return False
    return False

N, Q = list(map(int, input().split()))
s = input()
comm = [input().split() for _ in range(Q)]

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_0(s, comm, mid):
        l = mid + 1
    else:
        r = mid - 1
left = r # いける右端index

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_N(s, comm, mid):
        r = mid - 1
    else:
        l = mid + 1
right = l # いける左端index
print(right-left-1)