Member-only story

Subsequence method: Verify if all n2 elements of a2 appear in a1, in the same order

Marika Lam
2 min readMay 4, 2022

bool subsequence(const string a1[], int n1, const string a2[], int n2);

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 so contain a2. (Of course, every sequence, even a sequence of 0 elements, contains a sequence of 0 elements.) Return false (instead of −1) if this function is passed any bad arguments. Here's an example:

string big[10] = { "elena", "john", "amy", "ketanji", "neil", "amy" };
string little1[10] = { "john", "ketanji", "neil" };
bool u1 = subsequence(big, 6, little1, 3); // returns true
string little2[10] = { "amy", "john" };
bool u2 = subsequence(big, 6, little2, 2); // returns false
string little3[10] = { "john", "amy", "amy" };
bool u3 = subsequence(big, 6, little3, 3); // returns true
string little4[10] = { "john", "john", "amy" };
bool u4 = subsequence(big, 6, little4, 3); // returns false
bool u5 = subsequence(big, 6, little4, 0); // returns true

Psuedocode

bool subsequence(const string a1[], int n1, const string a2[], int n2){int countMatchingStrings=0;for (int i=0; i < n1; i++){for (int j=0; j<n2; j++){//check if a1[i] is equal to a2[j]//if so, increase the counter: countMatchingStrings+1//break;

--

--

No responses yet