Skip to content

Instantly share code, notes, and snippets.

@amitdu6ey
Created September 22, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitdu6ey/21a5b792b514e2407f0402c323519ecb to your computer and use it in GitHub Desktop.
Save amitdu6ey/21a5b792b514e2407f0402c323519ecb to your computer and use it in GitHub Desktop.
// [amitdu6ey]
// g++ -std=c++11 -o2 -Wall filename.cpp -o filename
#include <bits/stdc++.h>
#define hell 1000000009
#define bug1(x) cout<<":"<<x
#define bug2(x) cout<<":"<<x
#define bug3(x) cout<<"# "<<x<<" #"<<endl
#define ll long long
#define pb push_back
#define mp make_pair
#define tr(z) for(auto it=z.begin(); it!=z.end(); it++)
#define rloop(i,a,b) for(long long i=a; i>=b;i--)
#define loop(i,a,b) for(long long i=a; i<b; i++)
#define vbool vector< bool >
#define vvbool vector< vector<bool> >
#define vchar vector<char>
#define vi vector<int>
#define vvi vector< vector<int> >
#define pll pair<long long, long long>
#define vll vector<long long>
#define vvl vector< vector<long long> >
#define ninf numeric_limits<long long>::min()
#define pinf numeric_limits<long long>::max()
#define comp(a,b) (abs(a-b)<1e-9) // to compare doubles
using namespace std;
ll n;
string s;
map<char, ll> f;
vchar digs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
vchar odds;
bool possible_left[10][100009];
bool possible_right[10][100009];
bool contains(ll l, ll r){
/*this function return true if all elements
in odds vector occur in this range in ~ O(1).*/
bool ans=true;
for(char odd : odds){
if(!possible_left[(int)(odd-'0')][r] or !possible_right[(int)(odd-'0')][l]){
return false;
}
}
return true;
}
bool is_found(ll len){
/* this function return true if any substring of length
len is posssible which contains all element of odds vector*/
for(ll i=0;i<n-len;i++){
bool ans=true;
ll l=i, r=i+len-1;
if(contains(l,r)){
return true;
}
}
return false;
}
void solve(){
cin>>n;
cin>>s;
loop(i,0,10){
loop(j,0,n+1){
possible_left[i][j]=false; //it tells whether number i is present on left of index j
possible_right[i][j]=false; //it tells whether number i is present on right of index j
}
}
for(int i=0;i<=9;i++){
for(int j=0;j<n;j++){
if((int)(s[j]-'0')==i){
loop(k,j,n) possible_left[i][k]=true;
break;
}
}
for(int j=n-1;j>=0;j--){
if((int)(s[j]-'0')==i){
loop(k,0,j+1) possible_right[i][k]=true;
break;
}
}
}
for(ll i=0;i<n;i++){
f[s[i]]++; // couinting frequency of every num
}
for(auto d : digs){
if(f[d]%2!=0 and f[d]!=0){
odds.pb(d); // if odd frequency put it in odds array
}
}
// binary search on length
ll l=0, r=n;
ll ans=pinf;
while(l<=r){
ll len=(l+r)/2;
if(is_found(len)){
r=len-1;
ans=min(ans, len);
}
else{
l=len+1;
}
}
// corner cases
if(odds.size()==0) ans=-1;
if(odds.size()==1) ans=0;
cout<<ans<<"\n";
return;
}
int main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int test_cases=1;
//cin>>test_cases;
while(test_cases--){
solve();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment