CF BUDDY
← Problems·

1509B · TMT Document

1100 · greedy

Problem: 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 nn 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 aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero) characters.

Input Format: The first line contains an integer tt (1t50001 \le t \le 5000)  — the number of test cases.

The first line of each test case contains an integer nn (3n<1053 \le n < 10^5), the number of characters in the string entered in the document. It is guaranteed that nn is divisible by 33.

The second line of each test case contains a string of length nn consisting of only the characters T and M.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise.

Note: 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.

Sample Cases

Case 1

Input

5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT

Output

YES
NO
YES
NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback