Validate Subsequence

Problem:

Example:

Solution:

function isValidSubsequence(array, sequence) {

let fIdx = 0
let sIdx = 0
while (fIdx < array.length && sIdx < sequence.length) {
if (array[fIdx] === sequence[sIdx]) sIdx++
fIdx++
}
return sIdx === sequence.length
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Helping Verbs in English

Document Object Model

Angular Directives- An Introduction

At the crossroads — structuring CSS classes

Javascript variable confusion: Passing by Reference vs. Passing by Value

React Js Atomic-design Components creator Script.

Using Node.js in Production (Part II)

POWER BI- Drillthrough And Tooltip

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nerly Ton

Nerly Ton

More from Medium

Small Generators Aren’t As Good As Solar For Emergency Preparedness (Part 3) — CleanTechnica

“A New Horizon of Hope for Myanmar”

Browns v. Bengals observations

Dr. Pamela Gunter-Smith: ‘York is a wonderful hidden gem. We’ve got to stop being so hidden.’