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 (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

ttft(a)ft(a)ft(b)ft(b)
aa1100
abab1111
abaaba2211
bb0011
baba1111

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

ttft(a)ft(a)ft(b)ft(b)
aa1100
abab1111
abbabb1122
bb0011
bbbb0022

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

ttft(a)ft(a)ft(b)ft(b)ft(c)ft(c)
aa110000
abab111100
abcabc111111
bb001100
bcbc001111
cc000011

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值