TMT Document
The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length $$$n$$$ whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) characters.
In the first test case, the string itself is already a sequence equal to TMT.In the third test case, we may partition the string into the subsequences TMTMTT. Both the bolded and the non-bolded subsequences are equal to TMT.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases.The first line of each test case contains an integer $$$n$$$ ($$$3 \le n < 10^5$$$), the number of characters in the string entered in the document. It is guaranteed that $$$n$$$ is divisible by $$$3$$$.The second line of each test case contains a string of length $$$n$$$ consisting of only the characters T and M.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise.
def main():
    for _ in range(int(input())):
        size = int(input())
        s = input()
        c_t = []
        c_m = []
        for i in range(size):
            if s[i] == 'M':
                c_m.append(i)
            else:
                c_t.append(i)
        size = len(c_m)
        if len(c_t) // 2 != size:
            print('NO')
        else:
            flag = True
            for i in range(size):
                if c_m[i] < c_t[i] or c_m[i] > c_t[i + size]:
                    flag = False
                    break
            print(('NO', 'YES')[flag])
if __name__ == '__main__':
    main()
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
    n = int(readline())
    s = list(str(readline()))
    (t, m) = ([], [])
    for (i, c) in enumerate(s):
        if c == 'T':
            t.append(i)
        else:
            m.append(i)
    if len(t) != 2 * len(m):
        print('NO')
        return
    for i in range(len(m)):
        if m[i] < t[i] or m[i] > t[i + len(m)]:
            print('NO')
            break
    else:
        print('YES')

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()
def solve(s):
    t = 0
    tm = 0
    tmt = 0
    for char in s:
        if char == 'T':
            if tm > 0:
                tm -= 1
                tmt += 1
            else:
                t += 1
        elif t > 0:
            t -= 1
            tm += 1
        elif tmt > 0:
            tmt -= 1
            tm += 2
        else:
            return False
    return t == 0 and tm == 0

def main():
    for _ in range(int(input())):
        n = int(input())
        s = input()
        print('YES' if solve(s) or solve(s[::-1]) else 'NO')
main()