pick toys problem

Solutions on MaxInterview for pick toys problem by the best coders in the world

showing results for - "pick toys problem"
Viktoria
20 Oct 2019
1//Its better to  understand the problem and write your own code :)
2#include <bits/stdc++.h>
3
4using namespace std;
5
6int main()
7{
8    string s;
9    cout<<"Enter the string:"<<endl;
10    cin>>s;
11    int k;
12    cout<<"Enter the type of toys you want to pick:"<<endl;
13    cin>>k;
14    map<char,int>mp;
15    int mx=INT_MIN;
16    int i=0;
17    int j=0;
18    int l=s.length();
19    while(j<l)
20    {
21        mp[s[j]]++;
22        if(int(mp.size())<k)
23        {
24            j++;
25        }
26        else if(int(mp.size())==k)
27        {
28            mx=max(mx,j-i+1);
29            j++;
30        }
31        else
32        {
33            while(int(mp.size())>k)
34            {
35                mp.erase(s[i]);
36                i++;
37            }
38            j++;
39        }
40    }
41    cout<<mx;
42    return 0;
43}
44