Codeforces Round 785 (Div. 2)
B
A Perfectly Balanced String?
Codeforces
1100
原题
← AC →
翻译中…

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

翻译中…

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 $s$ consisting of lowercase English letters only. Your task is to determine whether $s$ is perfectly balanced or not.

翻译中…

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

翻译中…

Input

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print "YES" if $s$ 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 $f_t(c)$ represent the frequency of character $c$ in string $t$.

翻译中…

For the first testcase we have

翻译中…
$t$ $f_t(a)$ $f_t(b)$
$a$ $1$ $0$
$ab$ $1$ $1$
$aba$ $2$ $1$
$b$ $0$ $1$
$ba$ $1$ $1$
翻译中…

It can be seen that for any substring $t$ of $s$, the difference between $f_t(a)$ and $f_t(b)$ is not more than $1$. Hence the string $s$ is perfectly balanced.

翻译中…

For the second testcase we have

翻译中…
$t$ $f_t(a)$ $f_t(b)$
$a$ $1$ $0$
$ab$ $1$ $1$
$abb$ $1$ $2$
$b$ $0$ $1$
$bb$ $0$ $2$
翻译中…

It can be seen that for the substring $t=bb$, the difference between $f_t(a)$ and $f_t(b)$ is $2$ which is greater than $1$. Hence the string $s$ is not perfectly balanced.

翻译中…

For the third testcase we have

翻译中…
$t$ $f_t(a)$ $f_t(b)$ $f_t(c)$
$a$ $1$ $0$ $0$
$ab$ $1$ $1$ $0$
$abc$ $1$ $1$ $1$
$b$ $0$ $1$ $0$
$bc$ $0$ $1$ $1$
$c$ $0$ $0$ $1$
翻译中…

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

翻译中…

样例

Input #1
5
aba
abb
abc
aaaaa
abcba
Output #1
YES
NO
YES
YES
NO
InputCopy OutputCopy
题目信息
题号
1673B
时间限制
1 s
内存限制
256 MB
标签
暴力
贪心
字符串

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