701C codeforces - They are everywhere


701C codeforces - They are everywhere






Solution in C++


#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
#define pii pair<int,int>

int n,sol,all,l,r;
string inp;
map<int,int> wor;
set<char> uni;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>inp;
    for(auto c : inp)uni.insert(c);
    all=uni.size();
    sol=n;
    while(1)
    {
        if(wor.size()<all && r<n)
            wor[ inp[r] ]++,
            r++;
        else if(wor.size()==all && l<r)
        {
            sol=min(sol,r-l);
            if(--wor[inp[l]]==0)wor.erase(inp[l]);
            l++;
        }
        else break;
    }
    cout<<sol;
    return 0;
}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق