CF1673B.A Perfectly Balanced String?

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

A Perfectly Balanced String?

Let's call a string ss perfectly balanced if for all possible triplets (t,u,v)(t,u,v) such that tt is a non-empty substring of ss and uu and vv are characters present in ss, the difference between the frequencies of uu and vv in tt is not more than 11.

For example, the strings "aba" and "abc" are perfectly balanced but "abb" is not because for the triplet ("bb",'a','b'), the condition is not satisfied.

You are given a string ss consisting of lowercase English letters only. Your task is to determine whether ss is perfectly balanced or not.

A string bb is called a substring of another string aa if bb can be obtained by deleting some characters (possibly 00) from the start and some characters (possibly 00) from the end of aa.

Input

The first line of input contains a single integer tt (1t21041\leq t\leq 2\cdot 10^4) denoting the number of testcases.

Each of the next tt lines contain a single string ss (1s21051\leq |s|\leq 2\cdot 10^5), consisting of lowercase English letters.

It is guaranteed that the sum of s|s| over all testcases does not exceed 21052\cdot 10^5.

Output

For each test case, print "YES" if ss is a perfectly balanced string, and "NO" otherwise.

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

Note

Let ft(c)f_t(c) represent the frequency of character cc in string tt.

For the first testcase we have

tt ft(a)f_t(a) ft(b)f_t(b)
aa 11 00
abab 11
abaaba 22
bb 00
baba 11

It can be seen that for any substring tt of ss, the difference between ft(a)f_t(a) and ft(b)f_t(b) is not more than 11. Hence the string ss is perfectly balanced.

For the second testcase we have

tt ft(a)f_t(a) ft(b)f_t(b)
aa 11 00
abab 11
abbabb 22
bb 00 11
bbbb 22

It can be seen that for the substring t=bbt=bb, the difference between ft(a)f_t(a) and ft(b)f_t(b) is 22 which is greater than 11. Hence the string ss is not perfectly balanced.

For the third testcase we have

tt ft(a)f_t(a) ft(b)f_t(b) ft(c)f_t(c)
aa 11 00 00
abab 11
abcabc 11
bb 00 00
bcbc 11
cc 00

It can be seen that for any substring tt of ss and any two characters u,v{a,b,c}u,v\in\{a,b,c\}, the difference between ft(u)f_t(u) and ft(v)f_t(v) is not more than 11. Hence the string ss is perfectly balanced.

Samples

5
aba
abb
abc
aaaaa
abcba
YES
NO
YES
YES
NO

在线编程 IDE

建议全屏模式获得最佳体验