Created
May 13, 2021 10:23
-
-
Save megabayt/e9bd74d8c60fd0811078b9b4f06690f5 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
function longestSubseq(s1, s2) { | |
const s1Arr = s1.split(''); | |
const s2Arr = s2.split(''); | |
let result = []; | |
while (s1Arr.length) { | |
const tmpResult = []; | |
let startIndex = 0; | |
s1Arr.forEach((s1Char, index) => { | |
const currentIndex = s2Arr.indexOf(s1Char, startIndex); | |
if (currentIndex !== -1) { | |
tmpResult.push(s1Char); | |
startIndex = currentIndex + 1; | |
} | |
}) | |
s1Arr.shift(); | |
if (tmpResult.length > result.length) { | |
result = tmpResult; | |
} | |
} | |
return result.join(''); | |
} | |
const answer = longestSubseq('AGGTAB', 'GXTXAYB'); | |
console.log(answer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment