[AlgoExpert] Validate Subsequence

Validate Subsequence

  • Time : O(min(n, m)) where n is size of array, m is size of sequence
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
using namespace std;

bool isValidSubsequence(vector<int> array, vector<int> sequence) {
int j = 0;
for(int i = 0; i < array.size() and j < sequence.size(); i++) {
if(array[i] == sequence[j]) j++;
}
return j == sequence.size();
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/28/PS/AlgoExpert/validate-subsequence/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.