I am supposed to make a function that can do the following:
function: bool subsequence(const string a1[], int n1, const string a2[], int n2)
Task:
If all n2 elements of a2 appear in a1, in the same order (though not necessarily consecutively), then return true. Return false if a1 does not contain a2 as a subsequence. (Of course, the empty sequence is a subsequence of any sequence.) Return false if this function is passed any bad arguments.
Here is what I tried and I have no idea why it won't work. Any help is appreciated!
bool subsequence(const string a1[], int n1, const string a2[], int n2)
{
if (n1 < 0 || n2 < 0 || n2 > n1) {
return false;
}
else if (n2 == 0) {
return true;
}
for (int i = 0; i < n1; i++) {
if (a1[i] == a2[0])
{
int j = 0;
for (int k = 0; k + i < n1; k++)
{
if (a1[i + k] == a2[j])
{
j++;
if (j = n2)
return true;
}
}
}
}
return false;
}