Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
x = [tuple(input().split()) for _ in range(q)]

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            r = m
idx1 = l

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            l = m
idx2 = r
print(max(n-(idx1+1)-(n-idx2), 0))


Fixed submission:
n, q = map(int, input().split())
s = input()
x = [tuple(input().split()) for _ in range(q)]

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            r = m
idx1 = l

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            l = m
idx2 = r
print(n-(idx1+1)-(n-idx2))

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
x = [tuple(input().split()) for _ in range(q)]

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            r = m
idx1 = l

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            l = m
idx2 = r
print(n-(idx1+1)-(n-idx2))


Fixed submission:
n, q = map(int, input().split())
s = input()
x = [tuple(input().split()) for _ in range(q)]

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            r = m
idx1 = l

l = 0
r = n
while r-l > 1:
    m = (l+r)//2
    c = s[m]
    idx = m
    for j in range(q):
        if x[j][0] == c:
            if idx == 0 and x[j][1] == "L":
                l = m
                break
            elif idx == n-1 and x[j][1] == "R":
                r = m
                break
            else:
                if x[j][1] == "L":
                    c = s[idx-1]
                    idx -= 1
                else:
                    c = s[idx+1]
                    idx += 1

        if j == q-1:
            l = m
idx2 = r
print(n-(idx1+1)-(n-idx2))