forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1509B.go
54 lines (50 loc) · 772 Bytes
/
1509B.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bufio"
. "fmt"
"io"
"strings"
)
// github.com/EndlessCheng/codeforces-go
func CF1509B(_r io.Reader, _w io.Writer) {
in := bufio.NewReader(_r)
out := bufio.NewWriter(_w)
defer out.Flush()
var T, n int
var s string
o:
for Fscan(in, &T); T > 0; T-- {
Fscan(in, &n, &s)
m := strings.Count(s, "M")
if m*3 != n {
Fprintln(out, "NO")
continue
}
c := 0
for _, b := range s {
if b == 'T' {
c++
} else {
c--
if c < 0 {
Fprintln(out, "NO")
continue o
}
}
}
c = 0
for i := n - 1; i >= 0; i-- {
if s[i] == 'T' {
c++
} else {
c--
if c < 0 {
Fprintln(out, "NO")
continue o
}
}
}
Fprintln(out, "YES")
}
}
//func main() { CF1509B(os.Stdin, os.Stdout) }