Skip to content

Instantly share code, notes, and snippets.

@megabayt
Created May 13, 2021 10:23
Show Gist options
  • Save megabayt/e9bd74d8c60fd0811078b9b4f06690f5 to your computer and use it in GitHub Desktop.
Save megabayt/e9bd74d8c60fd0811078b9b4f06690f5 to your computer and use it in GitHub Desktop.
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