Codeforces Round #833 (Div. 2) B. Diverse Substrings
In a diverse string, there are at most 10 10 10 distinct characters: ‘0’, ‘1’, … … …, ‘9’. Therefore, each of these characters can appear at most 10 10 10 times in a diverse string.
With all this in mind, the maximum possible length of a diverse string is 1 0 2 = 100 10^2=100 102=100. To solve this problem, we only need to check whether each substring of length l ≤ 100 l≤100 l≤100 is diverse.
Time complexity per testcase: O ( n ⋅ 1 0 2 ) O(n⋅10^2) O(n⋅102)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int T;cin>>T;
while(T--)
{
int n;cin>>n;
string s;cin>>s;
int res=0;
for(int i=0;i<n;i++)
{
vector<int> d(10);
int mx=0,len=0;
for(int j=i;j<min(i+100,n);j++)
{
if(d[s[j]-'0']==0) len++;
d[s[j]-'0']++;
mx=max(mx,d[s[j]-'0']);
if(mx<=len) res++;
}
}
cout<<res<<endl;
}
return 0;
}