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 (1≤t≤2⋅1041≤t≤2⋅104) denoting the number of testcases.
Each of the next tt lines contain a single string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105), consisting of lowercase English letters.
It is guaranteed that the sum of |s||s| over all testcases does not exceed 2⋅1052⋅105.
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).
Example
input
Copy
5 aba abb abc aaaaa abcba
output
Copy
YES NO YES YES NO
Note
Let ft(c)ft(c) represent the frequency of character cc in string tt.
For the first testcase we have
tt | ft(a)ft(a) | ft(b)ft(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)ft(a) and ft(b)ft(b) is not more than 11. Hence the string ss is perfectly balanced.
For the second testcase we have
tt | ft(a)ft(a) | ft(b)ft(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)ft(a) and ft(b)ft(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)ft(a) | ft(b)ft(b) | ft(c)ft(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∈{a,b,c}, the difference between ft(u)ft(u) and ft(v)ft(v) is not more than 11. Hence the string ss is perfectly balanced.
思路:首先我们从题中可以读出当一个字母出现两次的时候就非常危险了,然后在想abababab,这种的字符串,因为每次都只取一部分,假设取得长度为2,又刚好是每种字母都有,他们的差永远符合要求,然后假如把ababababab序列多扩展几个字母呢假如把ab重复变成abcde呢?因为这个周期中不重复,所以差值不会大于1,所以我们是不是这要找到周期就行了,然后何为周期呢?这要不出现重复的就是一个周期。
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=2e5+10;
void solve()
{
string s;
cin>>s;
int len=s.size();
int idx=0;
set<char>c;
for(idx=0;idx<len;idx++)
{
if(c.find(s[idx])==c.end())
{
c.insert(s[idx]);
}
else
{
break;
}
}
bool ok=true;
for(int i=idx;i<len;i++)
{
if(s[i]!=s[i-idx])
{
ok=false;
}
}
if(ok)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}