Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.