Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created August 18, 2020 05:22
Show Gist options
  • Save misterpoloy/7426e48ba2aca1e3dd0e47253cc693b5 to your computer and use it in GitHub Desktop.
Save misterpoloy/7426e48ba2aca1e3dd0e47253cc693b5 to your computer and use it in GitHub Desktop.
Validate Subsequence
using namespace std;
// Repasado 2 veces
bool isValidSubsequence(vector<int> array, vector<int> sequence) {
int i = 0;
int seqIndex = 0;
while (i < array.size()) {
cout << "top= " << array[i] << endl;
cout << "sequence= " << sequence[seqIndex] << endl;
if (array[i] == sequence[seqIndex]) {
i++;
seqIndex++;
cout << "SIIIIIIIIII total= " << seqIndex << endl;
} else {
i++;
}
}
cout << "sequence size" << sequence.size() << endl;
return seqIndex == sequence.size();
}
@misterpoloy
Copy link
Author

Valid Subsequence

Validate Subsequence

(Determine wether a sequence is a subsequence of the original array) eg, [a, c, d, f] it's a subsequence of [a, b, c, d, e, f].
**hint:**We can use pointers, a left for the subsequence and a second for the final array, if don't increment the pointer of the array.

// sample input:
array = [5, 1, 22, 25, 6, -1, 8, 10 ]
sequence = [1, 6, -1, 10]

// Sample output
True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment