Skip to content

Commit d17703d

Browse files
authored
Add function to find longest uncommon subsequence
1 parent ac6c18c commit d17703d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {number}
4+
*/
5+
var findLUSlength = function(strs) {
6+
let ans = -1;
7+
8+
function isSubsequence(a, b) {
9+
let i = 0;
10+
11+
for (let j = 0; j < b.length; j++) {
12+
if (a[i] === b[j]) {
13+
i++;
14+
}
15+
}
16+
17+
return i === a.length;
18+
}
19+
20+
for (let i = 0; i < strs.length; i++) {
21+
let uncommon = true;
22+
23+
for (let j = 0; j < strs.length; j++) {
24+
if (i === j) continue;
25+
26+
// strs[i]가 다른 문자열의 부분수열이면 탈락
27+
if (isSubsequence(strs[i], strs[j])) {
28+
uncommon = false;
29+
break;
30+
}
31+
}
32+
33+
if (uncommon) {
34+
ans = Math.max(ans, strs[i].length);
35+
}
36+
}
37+
38+
return ans;
39+
};

0 commit comments

Comments
 (0)