String window :Ravi has been given two strings named string1 and string 2. He is supposed to find the minimum length substring of the string1 which contains all the characters of string2. Help him to find the substring

//giving runerror in 2 cases

#include<bits/stdc++.h>
using namespace std;
int main() {
string str1,str2;
long int i,i1,i2,v,n,ans,f1,f2;
ans=1000000;
long int M1[2000]={0};
long int M2[2000]={0};
getline (std::cin,str1);
getline (std::cin,str2);
//cout<<str1<<endl;
for(i=0;i<str2.length();i++)
{
M2[str2[i]]++;
}
n=str1.length();
i1=0;
i2=0;v=0;
while(i2!=n)
{

   M1[str1[i2]]++;

   if(M1[str1[i2]]<=M2[str1[i2]])
   {
     
       v++;}

//cout<<M1[str1[i1]]<<"+"<<M2[str1[i1]]<<endl;
while(M1[str1[i1]]>M2[str1[i1]])
{

M1[str1[i1]]--;
 i1++;

}
if(str2.length()<=v)
{
if(i2-i1<ans)
{
ans=i2-i1;
f1=i1;
f2=i2;
//cout<<f1<<f2<<endl;
}
}

i2++;

}

cout<<str1.substr(f1,f2-f1+1);

return 0;

}
//giving runerror in 2 cases

Share code via CB IDE with indentation and comments please

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.