Created
September 24, 2019 16:29
-
-
Save marktellez/7c667755dadc25815fa03f56bdc683a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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