Longest Uncommon Subsequence II LeetCode solution in JavaScript

1
var findLUSlength = function (strs) {
2
strs.sort((a, b) => b.length - a.length);
3
const isSubsequence = (a, b) => {
4
const A_LENGTH = a.length;
5
const B_LENGTH = b.length;
6
if (A_LENGTH > B_LENGTH) return false;
7
if (a === b) return true;
8
const matches = [...b].reduce((pos, str) => {
9
return a[pos] === str ? pos + 1 : pos;
10
}, 0);
11
return matches === A_LENGTH;
12
};
13

14
for (let a = 0; a < strs.length; a++) {
15
let isUncommon = true;
16

17
for (let b = 0; b < strs.length; b++) {
18
if (a === b) continue;
19
if (isSubsequence(strs[a], strs[b])) {
20
isUncommon = false;
21
break;
22
}
23
}
24
if (isUncommon) return strs[a].length;
25
}
26
return -1;
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0