Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        # 呪文
        t,d = input().split()
        command.append([t,d])
    r_ans = N
    l_ans = -1
    for i in range(N):
        if simulation_R(command,s,i,N):
            r_ans = i
            break
    # print(r_ans)


    for i in range(N-1,-1,-1):
        if simulation_L(command,s,i,N):
            l_ans = i
            break
    # print(l_ans)

    ans = N - (N-r_ans) - (l_ans+1)
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


# def simulation(command,s,now,d,N):
#     # print("test"+str(now))
#     for pair in command:
#         if pair[0] == s[now]:
#             if pair[1] == 'L':
#                 now -= 1
#             elif pair[1] == 'R':
#                 now += 1
#         if d == 'L' and now < 0 :
#             return True
#         elif
#         elif d == 'R' and now >= N :
#             return True
#     return False


if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        t,d = input().split()
        command.append([t,d])

    r = binary_serch_R(command,s,N)
    #print(r)
    l = binary_serch_L(command,s,N)
    #print(l)
    ans = ans - (N-r) - (l+1)
    if ans < 0:
        ans = 0
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


def binary_serch_R(command,s,N):
    left = 0
    right = N-1
    if simulation_R(command,s,left,N) :
        return -1
    if simulation_R(command,s,right,N) == False:
        return N

    while abs(right - left) > 1:
        mid = int((left+right)/2)
        # print(mid)
        if simulation_R(command,s,mid,N) :
            right = mid
        else :
            left = mid 
    return right

def binary_serch_L(command,s,N):
    left = 0
    right = N-1
    if simulation_L(command,s,left,N) == False :
        return -1
    if simulation_L(command,s,right,N):
        return N
        
    while abs(right-left) > 1:
        mid = int((left+right)/2)
        if simulation_L(command,s,mid,N) :
            left = mid
        else :
            right = mid 
    return left

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        # 呪文
        t,d = input().split()
        command.append([t,d])
    r_ans = N
    l_ans = -1
    for i in range(N):
        if simulation(command,s,i,'R',N):
            r_ans = i
            break
    # print(r_ans)


    for i in range(N-1,-1,-1):
        if simulation(command,s,i,'L',N):
            l_ans = i
            break
    # print(l_ans)

    ans = N - (N-r_ans) - (l_ans+1)
    print(ans)


def simulation(command,s,now,d,N):
    # print("test"+str(now))
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if d == 'L' and now < 0 :
            return True
        elif d == 'R' and now >= N :
            return True
    return False


def move(Golem_list,d,ans,N,si):
    if d == 'L':
        if si == 0:
            ans -= Golem_list[0]
            Golem_list[0] = 0
        else :
            Golem_list[si-1] += Golem_list[si]
            Golem_list[si] = 0

    elif d == 'R':
        if si == N-1:
            ans -= Golem_list[N-1]
            Golem_list[N-1] = 0
        else :
            Golem_list[si+1] += Golem_list[si]
            Golem_list[si] = 0
    # print(Golem_list)
    # print("#"+str(ans))
    return ans

if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        t,d = input().split()
        command.append([t,d])

    r = binary_serch_R(command,s,N)
    #print(r)
    l = binary_serch_L(command,s,N)
    #print(l)
    ans = ans - (N-r) - (l+1)
    if ans < 0:
        ans = 0
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


def binary_serch_R(command,s,N):
    left = 0
    right = N-1
    if simulation_R(command,s,left,N) :
        return -1
    if simulation_R(command,s,right,N) == False:
        return N

    while abs(right - left) > 1:
        mid = int((left+right)/2)
        # print(mid)
        if simulation_R(command,s,mid,N) :
            right = mid
        else :
            left = mid 
    return right

def binary_serch_L(command,s,N):
    left = 0
    right = N-1
    if simulation_L(command,s,left,N) == False :
        return -1
    if simulation_L(command,s,right,N):
        return N
        
    while abs(right-left) > 1:
        mid = int((left+right)/2)
        if simulation_L(command,s,mid,N) :
            left = mid
        else :
            right = mid 
    return left

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    Golem_list = [1] * N
    ans = N
    char_list = {}
    for si in range(len(s)):
        if s[si] in char_list:
            char_list[s[si]].append(si)
        else:
            char_list[s[si]] = [si]

    # print(char_list)

    for i in range(Q):
        # 呪文
        t,d = input().split()
        if t in char_list:
            tlist = char_list[t]
            for si in tlist:
                ans = move(Golem_list,d,ans,N,si)
    print(ans)


def move(Golem_list,d,ans,N,si):
    if d == 'L':
        if si == 0:
            ans -= Golem_list[0]
            Golem_list[0] = 0
        else :
            Golem_list[si-1] += Golem_list[si]
            Golem_list[si] = 0

    elif d == 'R':
        if si == N-1:
            ans -= Golem_list[N-1]
            Golem_list[N-1] = 0
        else :
            Golem_list[si+1] += Golem_list[si]
            Golem_list[si] = 0
    # print(Golem_list)
    # print("#"+str(ans))
    return ans

if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        t,d = input().split()
        command.append([t,d])

    r = binary_serch_R(command,s,N)
    #print(r)
    l = binary_serch_L(command,s,N)
    #print(l)
    ans = ans - (N-r) - (l+1)
    if ans < 0:
        ans = 0
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


def binary_serch_R(command,s,N):
    left = 0
    right = N-1
    if simulation_R(command,s,left,N) :
        return -1
    if simulation_R(command,s,right,N) == False:
        return N

    while abs(right - left) > 1:
        mid = int((left+right)/2)
        # print(mid)
        if simulation_R(command,s,mid,N) :
            right = mid
        else :
            left = mid 
    return right

def binary_serch_L(command,s,N):
    left = 0
    right = N-1
    if simulation_L(command,s,left,N) == False :
        return -1
    if simulation_L(command,s,right,N):
        return N
        
    while abs(right-left) > 1:
        mid = int((left+right)/2)
        if simulation_L(command,s,mid,N) :
            left = mid
        else :
            right = mid 
    return left

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    Golem_list = [1] * N
    ans = N
    for i in range(Q):
        # 呪文
        t,d = input().split()
        for si in range(len(s)):
            if s[si] == t:#マス目の指定
                ans = move(Golem_list,d,ans,N,si)
    print(ans)


def move(Golem_list,d,ans,N,si):
    if d == 'L':
        if si == 0:
            ans -= Golem_list[0]
            Golem_list[0] = 0
        else :
            Golem_list[si-1] += Golem_list[si]
            Golem_list[si] = 0

    elif d == 'R':
        if si == N-1:
            ans -= Golem_list[N-1]
            Golem_list[N-1] = 0
        else :
            Golem_list[si+1] += Golem_list[si]
            Golem_list[si] = 0
    # print(Golem_list)
    # print("#"+str(ans))
    return ans

if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        t,d = input().split()
        command.append([t,d])

    r = binary_serch_R(command,s,N)
    #print(r)
    l = binary_serch_L(command,s,N)
    #print(l)
    ans = ans - (N-r) - (l+1)
    if ans < 0:
        ans = 0
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


def binary_serch_R(command,s,N):
    left = 0
    right = N-1
    if simulation_R(command,s,left,N) :
        return -1
    if simulation_R(command,s,right,N) == False:
        return N

    while abs(right - left) > 1:
        mid = int((left+right)/2)
        # print(mid)
        if simulation_R(command,s,mid,N) :
            right = mid
        else :
            left = mid 
    return right

def binary_serch_L(command,s,N):
    left = 0
    right = N-1
    if simulation_L(command,s,left,N) == False :
        return -1
    if simulation_L(command,s,right,N):
        return N
        
    while abs(right-left) > 1:
        mid = int((left+right)/2)
        if simulation_L(command,s,mid,N) :
            left = mid
        else :
            right = mid 
    return left

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
def main():
    N,Q = int(input().split())
    s = input()
    Golem_list = [1] * N
    ans = N
    for i in range(Q):
        # 呪文
        t,d = input().split()
        for si in range(len(s)):
            if s[si] == t:#マス目の指定
                    move(Golem_list,d,ans,N,si)
                # if d == 'L':
                #     move(Golem_list,d,ans)
                #     print()#左に移動
                # elif d == 'R':
                #     print()#右に移動

                    # はみ出る場合
    print(ans)


def move(Golem_list,d,ans,N,si):
    if d == 'L':
        if si == 0:
            ans -= Golem_list[0]
            Golem_list[0] = 0
        else :
            Golem_list[si-1] += Golem_list[si]
            Golem_list[si] = 0

    elif d == 'R':
        if si == N-1:
            ans -= Golem_list[N-1]
            Golem_list[N-1] = 0
        else :
            Golem_list[si+1] += Golem_list[si]
            Golem_list[si] = 0


if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
def main():
    N,Q = map(int, input().split())
    s = input()
    ans = N
    command = []

    # print(char_list)

    for i in range(Q):
        t,d = input().split()
        command.append([t,d])

    r = binary_serch_R(command,s,N)
    #print(r)
    l = binary_serch_L(command,s,N)
    #print(l)
    ans = ans - (N-r) - (l+1)
    if ans < 0:
        ans = 0
    print(ans)

def simulation_R(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return False
        elif now >= N:
            return True
    return False

def simulation_L(command,s,now,N):
    for pair in command:
        if pair[0] == s[now]:
            if pair[1] == 'L':
                now -= 1
            elif pair[1] == 'R':
                now += 1
        if now < 0:
            return True
        elif now >= N:
            return False
    return False


def binary_serch_R(command,s,N):
    left = 0
    right = N-1
    if simulation_R(command,s,left,N) :
        return -1
    if simulation_R(command,s,right,N) == False:
        return N

    while abs(right - left) > 1:
        mid = int((left+right)/2)
        # print(mid)
        if simulation_R(command,s,mid,N) :
            right = mid
        else :
            left = mid 
    return right

def binary_serch_L(command,s,N):
    left = 0
    right = N-1
    if simulation_L(command,s,left,N) == False :
        return -1
    if simulation_L(command,s,right,N):
        return N
        
    while abs(right-left) > 1:
        mid = int((left+right)/2)
        if simulation_L(command,s,mid,N) :
            left = mid
        else :
            right = mid 
    return left

if __name__ == "__main__":
    main()