Buggy python submission (verdict: Time Limit Exceeded):
import copy
N, Q = map(int, input().split())
s = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]

# どんどん合体していくことだけはわかった
hash = {}
for i in range(N):
    if s[i] in hash.keys():
        hash[s[i]].append(i)
    else:
        hash[s[i]] = [i]

ans = 0
for t, d in TD:
    if t not in hash.keys():
        continue
    if d == 'R':
        r_list = map(lambda x: x+1, hash[t])
        hash[t] = []
        for r in r_list:
            if 0 <= r <= N-1:
                hash[s[r]].append(r)
            else:
                ans += 1
    else:
        l_list = map(lambda x: x-1, hash[t])
        hash[t] = []
        for l in l_list:
            if 0 <= l <= N - 1:
                hash[s[l]].append(l)
            else:
                ans += 1


print(N - ans)

Fixed submission:
N, Q = map(int, input().split())
S = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]


def disappear(index, place):
    if index == -1:
        return (False, False)
    else:
        left = False
        right = False
        for t, d in TD:
            cur_chara = place[index]
            if t == cur_chara:
                if d == 'R':
                    index += 1
                else:
                    index -= 1
                if index < 0:
                    left = True
                    break
                elif index > N-1:
                    right = True
                    break
        return (left, right)


def binarySearch(place, direction):
    """
    :param direction; left, right
    :return: 消失する位置
    """
    start, end = -1, N
    if direction == 'left':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[0]:
                start = t
            else:
                end = t
        return start
    elif direction == 'right':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[1]:
                end = t
            else:
                start = t
        return start


left = binarySearch(S, direction='left')
right = binarySearch(S, direction='right')
print(right - left)

Buggy python submission (verdict: Time Limit Exceeded):
import copy
N, Q = map(int, input().split())
s = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]

# どんどん合体していくことだけはわかった
hash = {}
for i in range(N):
    if s[i] in hash.keys():
        hash[s[i]].append(i)
    else:
        hash[s[i]] = [i]

for t, d in TD:
    if t not in hash.keys():
        continue
    if d == 'R':
        r_list = map(lambda x: x+1, hash[t])
        hash[t] = []
        for r in r_list:
            if 0 <= r <= N-1:
                hash[s[r]].append(r)
    else:
        l_list = map(lambda x: x-1, hash[t])
        hash[t] = []
        for l in l_list:
            if 0 <= l <= N - 1:
                hash[s[l]].append(l)

ans = 0
for d in hash.values():
    ans += len(d)
print(ans)

Fixed submission:
N, Q = map(int, input().split())
S = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]


def disappear(index, place):
    if index == -1:
        return (False, False)
    else:
        left = False
        right = False
        for t, d in TD:
            cur_chara = place[index]
            if t == cur_chara:
                if d == 'R':
                    index += 1
                else:
                    index -= 1
                if index < 0:
                    left = True
                    break
                elif index > N-1:
                    right = True
                    break
        return (left, right)


def binarySearch(place, direction):
    """
    :param direction; left, right
    :return: 消失する位置
    """
    start, end = -1, N
    if direction == 'left':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[0]:
                start = t
            else:
                end = t
        return start
    elif direction == 'right':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[1]:
                end = t
            else:
                start = t
        return start


left = binarySearch(S, direction='left')
right = binarySearch(S, direction='right')
print(right - left)

Buggy python submission (verdict: Time Limit Exceeded):
import copy
N, Q = map(int, input().split())
s = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]

# どんどん合体していくことだけはわかった
hash = {}
for i in range(N):
    if s[i] in hash.keys():
        hash[s[i]].append(i)
    else:
        hash[s[i]] = [i]

for t, d in TD:
    if t not in hash.keys():
        continue
    if d == 'R':
        r_list = [i+1 for i in hash[t]]
        hash[t] = []
        for r in r_list:
            if 0 <= r <= N-1:
                hash[s[r]].append(r)
    else:
        l_list = [i-1 for i in hash[t]]
        hash[t] = []
        for l in l_list:
            if 0 <= l <= N - 1:
                hash[s[l]].append(l)

ans = 0
for d in hash.values():
    ans += len(d)
print(ans)

Fixed submission:
N, Q = map(int, input().split())
S = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]


def disappear(index, place):
    if index == -1:
        return (False, False)
    else:
        left = False
        right = False
        for t, d in TD:
            cur_chara = place[index]
            if t == cur_chara:
                if d == 'R':
                    index += 1
                else:
                    index -= 1
                if index < 0:
                    left = True
                    break
                elif index > N-1:
                    right = True
                    break
        return (left, right)


def binarySearch(place, direction):
    """
    :param direction; left, right
    :return: 消失する位置
    """
    start, end = -1, N
    if direction == 'left':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[0]:
                start = t
            else:
                end = t
        return start
    elif direction == 'right':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[1]:
                end = t
            else:
                start = t
        return start


left = binarySearch(S, direction='left')
right = binarySearch(S, direction='right')
print(right - left)

Buggy python submission (verdict: Runtime Error):
import copy
N, Q = map(int, input().split())
s = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]

# どんどん合体していくことだけはわかった
hash = {}
for i in range(N):
    if s[i] in hash.keys():
        hash[s[i]].append(i)
    else:
        hash[s[i]] = [i]

for t, d in TD:
    if d == 'R':
        r_list = [i+1 for i in hash[t]]
        hash[t] = []
        for r in r_list:
            if 0 <= r <= N-1:
                hash[s[r]].append(r)
    else:
        l_list = [i-1 for i in hash[t]]
        hash[t] = []
        for l in l_list:
            if 0 <= l <= N - 1:
                hash[s[l]].append(l)

ans = 0
for d in hash.values():
    ans += len(d)
print(ans)

Fixed submission:
N, Q = map(int, input().split())
S = str(input())
TD = [list(map(str, input().split())) for i in range(Q)]


def disappear(index, place):
    if index == -1:
        return (False, False)
    else:
        left = False
        right = False
        for t, d in TD:
            cur_chara = place[index]
            if t == cur_chara:
                if d == 'R':
                    index += 1
                else:
                    index -= 1
                if index < 0:
                    left = True
                    break
                elif index > N-1:
                    right = True
                    break
        return (left, right)


def binarySearch(place, direction):
    """
    :param direction; left, right
    :return: 消失する位置
    """
    start, end = -1, N
    if direction == 'left':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[0]:
                start = t
            else:
                end = t
        return start
    elif direction == 'right':
        while end - start > 1:
            t = start + (end - start) // 2
            if disappear(t, place)[1]:
                end = t
            else:
                start = t
        return start


left = binarySearch(S, direction='left')
right = binarySearch(S, direction='right')
print(right - left)