#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,t;
getline(cin,s);
getline(cin,t);
map<char,int> freq_t,freq_s;
int n=s.size(),m=t.size();
int resl=-1,resr=1e9;
for(auto x:t)
freq_t[x]++;
int l=0,r=0;
for(r=0;r<n;r++)
{
freq_s[s[r]]++;
bool good=true;
for(auto x:freq_t)
{
if(freq_s.count(x.first)==0||(freq_s[x.first]<x.second))// count function tells us that if the key is present in the map it gives 1 if present else 0;
{
good=false;
break;
}
}
if(!good)
continue;
// if good
while(l<n && l<=r &&(freq_t.count(s[l])==0)||(freq_s[s[l]]>freq_t[s[l]]))
{
freq_s[s[l]]--;
if(freq_s[s[l]]==0)
freq_s.erase(s[l]);
l++;
}
if((resr-resl+1)>(r-l+1))
{
resl=l;
resr=r;
}
}
cout<<s.substr(resl,(resr-resl+1));
return 0;
}
i’m getting a run error in test case 2 and 7