Is sub-sequence | Rust

Micheal Keines
2 min readOct 2, 2021

Write a function that validates if a sub-sequence exists in the main array

A sub-sequence is formed by elements that are in same order but may not be adjacent to each other.

Sub-sequence

we have to go through every element in the main array thus, the Time complexity will be O(n) and Space O(1)

Rust code

Code available here

With i as the index for sub-seq array, we will traverse through the main array, if current element in main array arr[i] == current (sub-seq[i]), we will increment the i value. thus, if we were to find the actual sequence in the main array the final count of i will be equal to the length of the sub-sequence array.

Additionally, we will check if ( i == length of sub-sequence) in the beginning of every iteration and if the iteration is over and i != length return False

Result

--

--