Memory and Time Limit Exceed


#1

Can anybody tell how to overcome problem of Memory and Time Limit for this question?


#2

My solution

vector Solution::solve(string A, vector &B) {
unordered_map<string,int> mp1,mp2;
for(int i=0;i<B.size();i++)
{
mp1[B[i]]++;
}
int m=B[0].length();
int n=A.length();
vector ans;
for(int i=0;i<n;i++)
{
string s="";
mp2=mp1;
for(int j=i;j<n;j++)
{
s=s+A[j];
if(mp2.find(s)!=mp2.end())
{
//cout<<s<<endl;
mp2[s]–;
if(mp2[s]==0)
mp2.erase(s);
s="";
}
else if(s.length()>=m)
break;
}
if(mp2.size()==0)
{
ans.push_back(i);
}
// cout<<endl;
}
return ans;
}