-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlongestSubseq.js
More file actions
36 lines (27 loc) · 767 Bytes
/
longestSubseq.js
File metadata and controls
36 lines (27 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Input: {"able", "ale", "apple", "bale", "kangaroo"}, "abppplee"
// Output: "apple"
const D = ['able', 'ale', 'apple', 'bale', 'kangaroo'];
const S = 'abppplee';
function longestSubseq(str, words) {
let longestWord = "";
words.forEach(eachWord => {
let notFound = false;
let strIndex = 0;
for (let i = 0; i < eachWord.length; i++) {
let charIndex = str.indexOf(eachWord[i], strIndex);
if (charIndex > -1) {
strIndex = charIndex;
} else {
notFound = true;
break;
}
}
if (!notFound) {
if (eachWord.length > longestWord.length) {
longestWord = eachWord;
}
}
});
return longestWord;
}
longestSubseq(S, D);