Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
s = input()
td = [tuple(input().split()) for i in range(Q)]

def left_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == -1):
            return True
    return False

def right_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == N):
            return False
    return True

def nibun(l, h, func):
    low = l
    high = h
    mid = (l+h)//2
    while low <= high:
        x = func(mid)
        if(x):
            low = mid+1
        else:
            high = mid-1
        mid = (low+high)//2
    return mid

print(nibun(0, N-1, right_check)-nibun(0, N-1, left_check))


Fixed submission:
N, Q = map(int, input().split())
s = input()
td = [tuple(input().split()) for i in range(Q)]

def left_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == N):
            return False
        elif(nowpos == -1):
            return True
    return False

def right_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == N):
            return False
        elif(nowpos == -1):
            return True
    return True

def nibun(l, h, func):
    low = l
    high = h
    mid = (l+h)//2
    while low <= high:
        x = func(mid)
        if(x):
            low = mid+1
        else:
            high = mid-1
        mid = (low+high)//2
    return mid

print(nibun(0, N-1, right_check)-nibun(0, N-1, left_check))

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

golems_in = dict([(i, 1) for i in range(N+2)])
golems_in[0] = 0
golems_in[N+1] = 0
alphabet_to_number = {}
for i in range(N):
    c = s[i]
    if not c in alphabet_to_number:
        alphabet_to_number[c] = [i + 1]
    else:
        alphabet_to_number[c].append(i + 1)

for i in range(Q):
    t, d = input().split()
    direction = 1 if d=="R" else -1
    n = len(alphabet_to_number[t]) if t in alphabet_to_number else 0
    for j in range(n):
        if(d == "R"):
            c = alphabet_to_number[t][n-j-1]
        else:
            c = alphabet_to_number[t][j]
        golems_in[c+direction] += golems_in[c]
        golems_in[c] = 0
print(N-golems_in[0]-golems_in[N+1])

Fixed submission:
N, Q = map(int, input().split())
s = input()
td = [tuple(input().split()) for i in range(Q)]

def left_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == N):
            return False
        elif(nowpos == -1):
            return True
    return False

def right_check(pos):
    nowpos = pos
    for t, d in td:
        if(d=="L" and t==s[nowpos]):
            nowpos -= 1
        elif(d=="R" and t==s[nowpos]):
            nowpos += 1
        if(nowpos == N):
            return False
        elif(nowpos == -1):
            return True
    return True

def nibun(l, h, func):
    low = l
    high = h
    mid = (l+h)//2
    while low <= high:
        x = func(mid)
        if(x):
            low = mid+1
        else:
            high = mid-1
        mid = (low+high)//2
    return mid

print(nibun(0, N-1, right_check)-nibun(0, N-1, left_check))