Buggy python submission (verdict: Time Limit Exceeded):
[N,Q] = list(map(int,input().split()))
s = str(input())
t = []
d = []
for i in range(Q):
    [t_temp,d_temp] = list(map(str,input().split()))
    t.append(t_temp)
    d.append(d_temp)

def move_golem(ini,s,t,d):
    golem_temp = ini
    for i in range(len(t)):
        if s[golem_temp] == t[i]:
            if d[i] == 'L':
                golem_temp -= 1
            elif d[i] == 'R':
                golem_temp += 1
        if golem_temp == -1 or golem_temp == len(s):
            break
    return golem_temp

# check lower bound

def check_lower(s,t,d):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,t,d)
        if mid_golem_temp == -1:
            lb = mid
        elif mid_golem_temp != -1:
            ub = mid
            
    return lb

def check_upper(s,t,d):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,t,d)
        if mid_golem_temp == len(s):
            ub = mid
        elif mid_golem_temp != len(s):
            lb = mid
            
    return ub

print(check_upper(s,t,d)-check_lower(s,t,d)-1)

Fixed submission:
[N,Q] = list(map(int,input().split()))
s = str(input())
magic = [input().split() for j in range(Q)]

def move_golem(ini,s,magic):
    golem_temp = ini
    for t,d in magic:
        if s[golem_temp] == t:
            if d == 'L':
                golem_temp -= 1
            elif d == 'R':
                golem_temp += 1
        if golem_temp == -1 or golem_temp == len(s):
            break
    return golem_temp

# check lower bound

def check_lower(s,magic):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,magic)
        if mid_golem_temp == -1:
            lb = mid
        elif mid_golem_temp != -1:
            ub = mid
            
    return lb

def check_upper(s,magic):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,magic)
        if mid_golem_temp == len(s):
            ub = mid
        elif mid_golem_temp != len(s):
            lb = mid
            
    return ub

print(check_upper(s,magic)-check_lower(s,magic)-1)

Buggy python submission (verdict: Time Limit Exceeded):
[N,Q] = list(map(int,input().split()))
s = str(input())
t = []
d = []
for i in range(Q):
    [t_temp,d_temp] = list(map(str,input().split()))
    t.append(t_temp)
    d.append(d_temp)

def check_golem(ini,s,t,d):
    golem_temp = ini
    for i in range(len(t)):
        if s[golem_temp] == t[i]:
            if d[i] == 'L':
                golem_temp -= 1
            elif d[i] == 'R':
                golem_temp += 1
        if golem_temp >= len(s) or golem_temp < 0:
            golem_temp = 'dame'
            break
    return golem_temp

data = [0]*len(s)
for i in range(len(s)):
    data[i] = check_golem(i,s,t,d)
    
ans = 0
for i in range(len(s)):
    if data[i] == 'dame':
        ans += 1

print(len(s)-ans)

Fixed submission:
[N,Q] = list(map(int,input().split()))
s = str(input())
magic = [input().split() for j in range(Q)]

def move_golem(ini,s,magic):
    golem_temp = ini
    for t,d in magic:
        if s[golem_temp] == t:
            if d == 'L':
                golem_temp -= 1
            elif d == 'R':
                golem_temp += 1
        if golem_temp == -1 or golem_temp == len(s):
            break
    return golem_temp

# check lower bound

def check_lower(s,magic):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,magic)
        if mid_golem_temp == -1:
            lb = mid
        elif mid_golem_temp != -1:
            ub = mid
            
    return lb

def check_upper(s,magic):
    lb = -1
    ub = len(s)
    while ub - lb > 1:
        mid = (lb + ub)//2
        mid_golem_temp = move_golem(mid,s,magic)
        if mid_golem_temp == len(s):
            ub = mid
        elif mid_golem_temp != len(s):
            lb = mid
            
    return ub

print(check_upper(s,magic)-check_lower(s,magic)-1)