Answers for "pick toys problem"

C++
0

pick toys problem

//Its better to  understand the problem and write your own code :)
#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cout<<"Enter the string:"<<endl;
    cin>>s;
    int k;
    cout<<"Enter the type of toys you want to pick:"<<endl;
    cin>>k;
    map<char,int>mp;
    int mx=INT_MIN;
    int i=0;
    int j=0;
    int l=s.length();
    while(j<l)
    {
        mp[s[j]]++;
        if(int(mp.size())<k)
        {
            j++;
        }
        else if(int(mp.size())==k)
        {
            mx=max(mx,j-i+1);
            j++;
        }
        else
        {
            while(int(mp.size())>k)
            {
                mp.erase(s[i]);
                i++;
            }
            j++;
        }
    }
    cout<<mx;
    return 0;
}
Posted by: Guest on June-29-2021

Browse Popular Code Answers by Language