Stuck with a problem

Print true if second string is a substring of first string, else print false

Note : & symbol can replace n number of characters
Input : Spoon Sp&n Output : TRUE
Mono &o&o Output : TRUE
Man n& Output : FALSE
Subline line Output : TRUE

I tried to solve it by matching each character but couldn’t able to cover the corner cases
Any suggestions?

2 Likes

#include

using namespace std;

int main(){
string s1=“zoho”;
string s2=“oo”;
bool matched = false;
int n1=s1.length(),n2=s2.length();
int i=0,j=0;
while(i<n1 && j<n2){
bool changed=false;
while(s2[j]==’*’ && i<n1 && j+1<n2 && s1[i]!=s2[j+1]){
i++;
changed = true;
}
if(changed) j++;
while(s1[i]!=s2[j]) i++;
while(i<n1 && j<n2 && s1[i]==s2[j]){
i++;
j++;
}
if(j==n2){
matched=true;
break;
}
}
if(matched) cout<<“Yes”;
else cout<<“No”;

return 0;

}