Codeforces Round 715 (Div. 2)
B
TMT Document
Codeforces
1100
原题
← AC →
翻译中…

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.

翻译中…

Input

翻译中…

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 \lt 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$.

翻译中…

Output

翻译中…

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.

翻译中…

样例

Input #1
5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT
Output #1
YES
NO
YES
NO
YES
InputCopy OutputCopy
题目信息
题号
1509B
时间限制
1 s
内存限制
256 MB
标签
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。