701C - They Are Everywhere - CodeForces Solution


binary search strings two pointers *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define el '\n'
#define dd long double
#define ff float
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define sc second
#define re reserve
#define re1 v.resize
#define re2 a.resize
#define in insert
#define lp(i, a, b) for(int i = a; i <= b; i++)
#define lp1(i, a, b) for(int i = a; i < b; i++)
#define ln(i,k,n)    for (int i=n ; i>=k ; i--)
#define ln1(i,k,n)    for (int i=n ; i>k ; i--)
using namespace std;
//continue
// @author: saleh_zizo
//std::reverse(b.begin(), b.end());
const ll mod = 1e9;
const ll N = 1e6+1;
bool prime[N];
//ll d[N];
//======================================================
void input()
{
#ifndef ONLINE_JUDGE
    freopen("islands.in","r",stdin);
    freopen("output.txt","w",stdout);
#endif
}
//======================================================
void fast() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
//======================================================
void sieve(){ // O(n * (log(log(n)))
    memset(prime, true, sizeof prime);
    prime[0] = prime[1] = false;
    for(int i = 2; i * i < N; i++){
        if(prime[i]){
            for(int j = i * i; j < N; j += i){
                prime[j] = false;
            }
        }
    }
}
//======================================================
pair<ll, ll>pi(ll h , ll m , ll cnt) {
	while (cnt--) {
		m++;
		if (m == 60) {h++; m = 0;}
		if (h == 24) {h = 0;}
	}
	return {h, m};
}
//======================================================
vector<int> divisors(int n){ // O(sqrt(n))
    vector<int>v;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0){
            v.push_back(i);
            if(n / i != i)
                v.push_back(n / i);
        }
    }
    return v;
}
//======================================================
vector<ll>primeFactorization(ll n){// O(sqrt(n))
    vector<ll>v;
    for(ll i=2;i*i<=n;i++){
       // vector<ll>temp;
        while(n%i==0){
            v.pb(i);
            n/=i;
        }
    }
    if(n>1){
        v.pb(n);
    }
    return v;
}
//======================================================
ll checked_true(ll mid){
    ll c = (mid*(mid+1))/2;
    return c;
}
//======================================================
ll F(ll n){
    if(n==0){
        return 0;
    }
    if(n==1){
        return 1;
    }
    return F(n-1) + F(n-2);
}
//======================================================
int main() {
fast();
// 0 1 2 [0 , 1 , 2 , '\0'];
//input()
//cout<<fixed<<setprecision(10);// 1 2 3 4 5
    //sieve();
    //    5
    // 1 2 3 0 3 8
    ll n; cin>>n;
    string x; cin>>x;
    map<char , ll> mp , temp;
    lp1(i,0,n){
        mp[x[i]]++;
    }
    ll l = 0 , r = 0 , ans = mod;
    while(l < n){
        while(temp.size() < mp.size() && r < n){
            temp[x[r++]]++;
        }
        if(temp.size() == mp.size()){
            //cout<<l<<' '<<r<<el;
            ans = min(ans , r - l);
        }
        temp[x[l]]--;
        if(temp[x[l]] == 0){
            temp.erase(x[l]);
        }
        l++;
    }
    cout<<ans<<el;
}


Comments

Submit
0 Comments
More Questions

1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones
218A - Mountain Scenery
486B - OR in Matrix
1405A - Permutation Forgery
1733A - Consecutive Sum
1733B - Rule of League
1733C - Parity Shuffle Sorting
1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment