#include
#include
#include
using namespace std;
int main()
{
string s ;
string t ;
getline(cin , s) ;//input the string 1
getline(cin , t) ;//input the string 2
int n = s.size() ;//getting the lenght of the string 1
int m = t.size() ;//getting the lenght of the string 2
int l=0, r;// the iterators to get the desired window
map<char, int> freq_s;//to store the frequencies of charaters in sring 1
map<char, int> freq_t;// to store the frequencies of charaters in sring 2
for(char x:t)
{
freq_t[x]++;//storing the frequencies of chars in string 2(the one we gotta check)
}
int resL =-1, resR = 1e9;
for(r=0 ; r<n ; r++)
{
// as we move r we store the frequencies of character in string1 in the map
freq_s[s[r]]++;
bool good = true;//checkk the condition for the window
for(auto x : freq_t)
{//the if condition checks of the character in string t is even present in the string s or frequency of the character is less than it should be
if((freq_s.count(x.first)==0) || (freq_s[x.first] < x.second)){
good = false;
break;
}
}
//if the window is not good then
if(!good) { continue; }
//other wise its a good window , and try to reduce the window by doing l++ and erasing the frequncy of the unnecessary characters from freq_s
while(l<n && l<=r && (freq_s.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++;
}
// cout<< l <<" "<<r<<"\n";//printing all the windows
if((resR - resL+1) > (r-l+1) )
{
resR =r;
resL= l;
}
}
// cout<<resL <<" "<<resR;
if(l == -1)
{
cout<<"No String";
}
else {
cout<<s.substr(resL, resR-resL+1);
}
}
this is my code but i cant get why the window is not right, i mean to say the value of r is not increasing after a ceratain point