Why this Code is giving Error

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

int main()
{
string s, t;
getline(cin, s);
getline(cin, t);

int n = s.size();
int m = t.size();

map<char, int> freq_s, freq_t;

for(auto x : t)
   freq_t[x]++;

int l = 0, r;
int resl = -1, resr = INT_MAX;

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){
            good = false;
            break;
        }
    }

    if(!good)
      continue;
    
    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(resl - resr + 1 > l - r + 1)
    {
        resl = l;
        resr = r;
    }
}

if(resr == -1)
   cout << "No String";
else
  cout << s.substr(resl, resr - resl + 1);

return 0;
}

error :- terminate called after throwing an instance of ‘std::out_of_range’ what(): basic_string::substr: __pos (which is 18446744073709551615) > this->size() (which is 20) /bin/run.sh: line 4: 18 Aborted (core dumped) ./exe

hi @devang_11912089
refer this code -->

#include<iostream>
#include <string>
#include<climits>
using namespace std;
string window(string s,string pat){
    int sl=s.length();
    int pl=pat.length();
    if(pl>sl){
        return "No String";

    }
    int fs[256]={0};
    int fp[256]={0};
    for(int i=0;i<pl;i++){
        char ch=pat[i];
        fp[ch]++;
    }
    int cnt=0;
    int start=0;
    int min_len=INT_MAX;
    int start_idx=-1;
    for(int i=0;i<sl;i++){
        char ch=s[i];
        fs[ch]++; 
        if(fp[ch]!=0 and fs[ch]<=fp[ch]){
            cnt++;
        }
        if(cnt==pl){
            char temp=s[start];
            while(fs[temp]==0 or fs[temp]>fp[temp]){
                fs[temp]--;
                start++;
                temp=s[start];
            }
            int window_len=i-start+1;
            if(window_len<min_len){
                min_len=window_len;
                start_idx=start;
            }
        }
    }
    if(start_idx==-1){
        return "No String";
    }
    string ans=s.substr(start_idx,min_len);
    return ans;
}
int main()
{
    string s;
    string p;
	getline(cin,s);
	getline(cin,p);
    cout<<window(s,p)<<endl;
    return 0;
}

hi @devang_11912089
i hope its clear now??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.