!Codeforces Round #364 (Div. 2) C. They Are Everywhere
https://codeforces.com/contest/701/problem/C
binary search strings two pointers
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; char s[N]; int main(){ int n,tot=0; cin>>n; getchar(); map<char,int>cnt; for(int i=1;i<=n;i++){ s[i]=getchar(); if(!cnt[s[i]])tot++; cnt[s[i]]++;} cnt.clear(); int r=0,ntot=0,ans=1e5+5,flag=0; for(int i=1;i<=n;i++){ // if(i!=1&&cnt[s[i-1]]==1) ntot--,cnt[s[i-1]]--; if(i!=1){ if(cnt[s[i-1]]==1)ntot--; cnt[s[i-1]]--; } while(ntot<tot){ if(r==n)break; r++; if(!cnt[s[r]])ntot++; cnt[s[r]]++; // if(r==n){flag=1;break; } } if(ntot==tot)ans=min(ans,r-i+1); //if(flag)break; } cout<<ans; return 0; }