Skip to content

Instantly share code, notes, and snippets.

@marktellez
Created September 24, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marktellez/7c667755dadc25815fa03f56bdc683a1 to your computer and use it in GitHub Desktop.
Save marktellez/7c667755dadc25815fa03f56bdc683a1 to your computer and use it in GitHub Desktop.
const tests = [
["ABAZDC", "BACBAD", "ABAD"],
["AGGTAB", "GXTXAYB", "GTAB"],
["aaa", "aa", "aa"],
["", "", ""],
["ABBA", "ABCABA", "ABBA"]
]
function longestSequence(s1, s2) {
const seq = []
const a1 = s1.split("")
const a2 = s2.split("")
for (let i = 0; i < a1.length; i++) {
if (!a1[i]) break
for (let j = 0; j < a2.length; j++) {
if (!a2[j]) break
if (a1[i] === a2[j]) {
seq.push(a2.splice(j, 1))
break
}
}
}
return seq
}
tests.forEach(test => {
console.log(`Running ${test[0]}:${test[1]}`)
const result = longestSequence(test[0], test[1]) !== test[2]
if (!result) {
throw `longestSequence("${test[0]}", "${test[1]}") was supposed to be "${
test[2]
}"`
}
console.log(`longestSequence("${test[0]}", "${test[1]}") === "${test[2]}"`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment