I'm a beginner working on an exercise where the aim is to count the amount of times two strings match. When we have two or more stars, multiple string combinations can be suitable. Here are some examples:
"abcbd" & "*b*" match twice : ("a","cbd") and ("abc", "d")
"abc" & "a**" match 3 times : (nothing,"bc"), ("b", "c") and ("bc", nothing)
int nummatch(char *s1, char *s2)
{
int i;
int matches;
if (*s1 == '\0' && *s2 == '\0')
return (1);
else if (*s2 == '*')
{
i = 0;
matches = 0;
while (s1[i] != '\0')
matches += nummatch(s1 + i++, s2 + 1);
matches += nummatch(s1 + i, s2 + 1);
return (matches);
}
else if (*s1 == *s2)
return (nummatch(s1 + 1, s2 + 1));
else
return (0);
}
Someone submitted this code and claims it works. I'm struggling to understand how it works. How does it account for overlap? What would happen with a combination explosion like xxxxxxxxxxxxxxxxxxxxa and *x*x*x*x*a were tested?