Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    pos = np.zeros((26, N), dtype=int)
    for i, c in enumerate(input().strip()):
        p = alph.index(c)
        pos[p][i] = 1

    cur = np.ones(N, dtype=int)
    for _ in range(Q):
        t, d = map(str, input().split())
        p = alph.index(t)
        m = pos[p]
        golem = cur * m

        tmp = golem.tolist()

        if d == 'L':
            tmp.append(0)
            moved_golem = np.array(tmp[1:])
        else:
            tmp = [0] + tmp
            moved_golem = np.array(tmp[:-1])

        cur = cur + moved_golem - golem

    print(np.sum(cur))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    order = [tuple(map(str, input().split())) for _ in range(Q)]
    order = order[::-1]

    left, right = 0, N-1
    for a in order:
        t, d = a[0], a[1]

        if d == 'L':
            if S[left] == t:
                left += 1
                if left > N - 1:
                    return 0

            if right < N - 1 and S[right + 1] == t:
                right = min(N - 1, right + 1)

        else:
            if left > 0 and S[left - 1] == t:
                left = max(0, left - 1)

            if S[right] == t:
                right -= 1
                if right < 0:
                    return 0

    return right - left + 1


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    pos = np.zeros((26, N), dtype=int)
    for i, c in enumerate(input().strip()):
        p = alph.index(c)
        pos[p][i] = 1

    cur = np.ones(N, dtype=int)
    for _ in range(Q):
        t, d = map(str, input().split())
        p = alph.index(t)
        m = pos[p]
        golem = cur * m

        if d == 'L':
            moved_golem = np.append(golem[1:], 0)
        else:
            moved_golem = np.insert(golem[:N-1], 0, 0)

        cur = cur + moved_golem - golem

    print(np.sum(cur))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    order = [tuple(map(str, input().split())) for _ in range(Q)]
    order = order[::-1]

    left, right = 0, N-1
    for a in order:
        t, d = a[0], a[1]

        if d == 'L':
            if S[left] == t:
                left += 1
                if left > N - 1:
                    return 0

            if right < N - 1 and S[right + 1] == t:
                right = min(N - 1, right + 1)

        else:
            if left > 0 and S[left - 1] == t:
                left = max(0, left - 1)

            if S[right] == t:
                right -= 1
                if right < 0:
                    return 0

    return right - left + 1


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    pos = [[0]*N for _ in range(26)]
    for i, c in enumerate(input().strip()):
        p = alph.index(c)
        pos[p][i] = 1

    move = []
    for p in pos:
        move.append(np.array(p))

    cur = np.ones(N, dtype=int)
    for _ in range(Q):
        t, d = map(str, input().split())
        p = alph.index(t)
        m = move[p]
        golem = cur * m

        if d == 'L':
            moved_golem = np.append(golem[1:], 0)
        else:
            moved_golem = np.insert(golem[:N-1], 0, 0)

        cur = cur + moved_golem - golem

    print(np.sum(cur))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    order = [tuple(map(str, input().split())) for _ in range(Q)]
    order = order[::-1]

    left, right = 0, N-1
    for a in order:
        t, d = a[0], a[1]

        if d == 'L':
            if S[left] == t:
                left += 1
                if left > N - 1:
                    return 0

            if right < N - 1 and S[right + 1] == t:
                right = min(N - 1, right + 1)

        else:
            if left > 0 and S[left - 1] == t:
                left = max(0, left - 1)

            if S[right] == t:
                right -= 1
                if right < 0:
                    return 0

    return right - left + 1


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    pos = [[0 for _ in range(N)] for _ in range(26)]
    for i, c in enumerate(input().strip()):
        p = alph.index(c)
        pos[p][i] = 1

    move = []
    for p in pos:
        move.append(np.array(p))

    cur = np.array([1 for _ in range(N)])
    for _ in range(Q):
        t, d = map(str, input().split())
        p = alph.index(t)
        m = move[p]
        golem = cur * m

        if d == 'L':
            moved_golem = np.append(golem[1:], 0)
        else:
            moved_golem = np.insert(golem[:N-1], 0, 0)

        cur = cur + moved_golem - golem

    print(np.sum(cur))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    order = [tuple(map(str, input().split())) for _ in range(Q)]
    order = order[::-1]

    left, right = 0, N-1
    for a in order:
        t, d = a[0], a[1]

        if d == 'L':
            if S[left] == t:
                left += 1
                if left > N - 1:
                    return 0

            if right < N - 1 and S[right + 1] == t:
                right = min(N - 1, right + 1)

        else:
            if left > 0 and S[left - 1] == t:
                left = max(0, left - 1)

            if S[right] == t:
                right -= 1
                if right < 0:
                    return 0

    return right - left + 1


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    pos = [[0 for _ in range(N)] for _ in range(26)]
    for i in range(len(S)):
        c = S[i]
        p = alph.index(c)
        pos[p][i] = 1

    move = []
    for p in pos:
        move.append(np.array(p))

    cur = np.array([1 for _ in range(N)])
    for _ in range(Q):
        t, d = map(str, input().split())
        p = alph.index(t)
        m = move[p]
        golem = cur * m
        cur = cur - golem

        if d == 'L':
            moved_golem = np.append(golem[1:], 0)
            cur = cur + moved_golem
        
        else:
            moved_golem = np.insert(golem[:N-1], 0, 0)
            cur = cur + moved_golem
            

    print(np.sum(cur))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = list(map(str, input().strip()))
    order = [tuple(map(str, input().split())) for _ in range(Q)]
    order = order[::-1]

    left, right = 0, N-1
    for a in order:
        t, d = a[0], a[1]

        if d == 'L':
            if S[left] == t:
                left += 1
                if left > N - 1:
                    return 0

            if right < N - 1 and S[right + 1] == t:
                right = min(N - 1, right + 1)

        else:
            if left > 0 and S[left - 1] == t:
                left = max(0, left - 1)

            if S[right] == t:
                right -= 1
                if right < 0:
                    return 0

    return right - left + 1


if __name__ == '__main__':
    print(main())