CF1673B.A Perfectly Balanced String?

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 .

输入格式

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 .

输出格式

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).

输入输出样例

  • 输入#1

    5
    aba
    abb
    abc
    aaaaa
    abcba

    输出#1

    YES
    NO
    YES
    YES
    NO

说明/提示

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 11 abaaba 22 11 bb 00 11 baba 11 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 11 abbabb 11 22 bb 00 11 bbbb 00 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 11 00 abcabc 11 11 11 bb 00 11 00 bcbc 00 11 11 cc 00 00 11 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.

首页