Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
indices = {}
for i in range(n):
    indices.setdefault(s[i], [])
    indices[s[i]].append(i)
g = [1 for _ in range(n)]
tds = []
for _ in range(q):
    tds.append(tuple(input().split()))
for t, d in tds:
    for i in indices.get(t, []):
        if d == 'L':
            if i != 0:
                g[i-1] += g[i]
        else:
            if i != n-1:
                g[i+1] += g[i]
        g[i] = 0
print(sum(g))

Fixed submission:
import sys
def vanish_on_left(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return True
            i -= 1
        else:
            if i == n-1:
                return False
            i += 1
    return False

def vanish_on_right(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return False 
            i -= 1
        else:
            if i == n-1:
                return True
            i += 1
    return False


n, q = map(int, input().split())
s = input()
tds = []
for _ in range(q):
    tds.append(tuple(input().split()))
if vanish_on_left(0, n, s, tds):
    if vanish_on_left(n-1, n, s, tds):
        print(0)
        sys.exit()
    l0, r0 = 1, n-1
    while l0 < r0:
        k = (l0 + r0) // 2
        if vanish_on_left(k, n, s, tds):
            l0 = k+1
        else:
            r0 = k
else:
    l0 = 0
if vanish_on_right(n-1, n, s, tds):
    if vanish_on_right(l0, n, s, tds):
        print(0)
        sys.exit()
    l1, r1 = l0, n-2
    while l1 < r1:
        k = (l1 + r1 + 1) // 2
        if vanish_on_right(k, n, s, tds):
            r1 = k-1
        else:
            l1 = k
else:
    l1 = n-1
print(l1 - l0 + 1)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
indices = {}
for i in range(n):
    indices.setdefault(s[i], [])
    indices[s[i]].append(i)
g = [1 for _ in range(n)]
for _ in range(q):
    t, d = input().split()
    for i in indices.get(t, []):
        if d == 'L':
            if i != 0:
                g[i-1] += g[i]
        else:
            if i != n-1:
                g[i+1] += g[i]
        g[i] = 0
print(sum(g))

Fixed submission:
import sys
def vanish_on_left(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return True
            i -= 1
        else:
            if i == n-1:
                return False
            i += 1
    return False

def vanish_on_right(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return False 
            i -= 1
        else:
            if i == n-1:
                return True
            i += 1
    return False


n, q = map(int, input().split())
s = input()
tds = []
for _ in range(q):
    tds.append(tuple(input().split()))
if vanish_on_left(0, n, s, tds):
    if vanish_on_left(n-1, n, s, tds):
        print(0)
        sys.exit()
    l0, r0 = 1, n-1
    while l0 < r0:
        k = (l0 + r0) // 2
        if vanish_on_left(k, n, s, tds):
            l0 = k+1
        else:
            r0 = k
else:
    l0 = 0
if vanish_on_right(n-1, n, s, tds):
    if vanish_on_right(l0, n, s, tds):
        print(0)
        sys.exit()
    l1, r1 = l0, n-2
    while l1 < r1:
        k = (l1 + r1 + 1) // 2
        if vanish_on_right(k, n, s, tds):
            r1 = k-1
        else:
            l1 = k
else:
    l1 = n-1
print(l1 - l0 + 1)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
indices = {}
for i in range(n):
    indices.setdefault(s[i], [])
    indices[s[i]].append(i)
g = [1 for _ in range(n)]
for _ in range(q):
    t, d = input().split()
    for i in indices.get(t, []):
        if s[i] == t:
            if d == 'L':
                if i != 0:
                    g[i-1] += g[i]
            else:
                if i != n-1:
                    g[i+1] += g[i]
            g[i] = 0
print(sum(g))

Fixed submission:
import sys
def vanish_on_left(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return True
            i -= 1
        else:
            if i == n-1:
                return False
            i += 1
    return False

def vanish_on_right(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return False 
            i -= 1
        else:
            if i == n-1:
                return True
            i += 1
    return False


n, q = map(int, input().split())
s = input()
tds = []
for _ in range(q):
    tds.append(tuple(input().split()))
if vanish_on_left(0, n, s, tds):
    if vanish_on_left(n-1, n, s, tds):
        print(0)
        sys.exit()
    l0, r0 = 1, n-1
    while l0 < r0:
        k = (l0 + r0) // 2
        if vanish_on_left(k, n, s, tds):
            l0 = k+1
        else:
            r0 = k
else:
    l0 = 0
if vanish_on_right(n-1, n, s, tds):
    if vanish_on_right(l0, n, s, tds):
        print(0)
        sys.exit()
    l1, r1 = l0, n-2
    while l1 < r1:
        k = (l1 + r1 + 1) // 2
        if vanish_on_right(k, n, s, tds):
            r1 = k-1
        else:
            l1 = k
else:
    l1 = n-1
print(l1 - l0 + 1)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
g = [1 for _ in range(n)]
for _ in range(q):
    t, d = input().split()
    for i in range(n):
        if s[i] == t:
            if d == 'L':
                if i != 0:
                    g[i-1] += g[i]
            else:
                if i != n-1:
                    g[i+1] += g[i]
            g[i] = 0
print(sum(g))

Fixed submission:
import sys
def vanish_on_left(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return True
            i -= 1
        else:
            if i == n-1:
                return False
            i += 1
    return False

def vanish_on_right(i, n, s, tds):
    for t, d in tds:
        if s[i] != t:
            continue
        if d == 'L':
            if i == 0:
                return False 
            i -= 1
        else:
            if i == n-1:
                return True
            i += 1
    return False


n, q = map(int, input().split())
s = input()
tds = []
for _ in range(q):
    tds.append(tuple(input().split()))
if vanish_on_left(0, n, s, tds):
    if vanish_on_left(n-1, n, s, tds):
        print(0)
        sys.exit()
    l0, r0 = 1, n-1
    while l0 < r0:
        k = (l0 + r0) // 2
        if vanish_on_left(k, n, s, tds):
            l0 = k+1
        else:
            r0 = k
else:
    l0 = 0
if vanish_on_right(n-1, n, s, tds):
    if vanish_on_right(l0, n, s, tds):
        print(0)
        sys.exit()
    l1, r1 = l0, n-2
    while l1 < r1:
        k = (l1 + r1 + 1) // 2
        if vanish_on_right(k, n, s, tds):
            r1 = k-1
        else:
            l1 = k
else:
    l1 = n-1
print(l1 - l0 + 1)