Skip to content

Commit a1d929a

Browse files
authored
Create 3076-shortest-uncommon-substring-in-an-array.js
1 parent c380c14 commit a1d929a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {string[]} arr
3+
* @return {string[]}
4+
*/
5+
var shortestSubstrings = function(arr) {
6+
function* gen(s) {
7+
const s_len = s.length
8+
for (let i = 0; i < s_len; i++) {
9+
for (let j = i; j < s_len; j++) {
10+
yield s.slice(i, j + 1)
11+
}
12+
}
13+
}
14+
15+
const ans = []
16+
const n = arr.length
17+
for (let i = 0; i < n; i++) {
18+
const cur_s = arr[i]
19+
let cur_ans = null
20+
for (const s of gen(cur_s)) {
21+
if (arr.filter((_, j) => j !== i).every((str) => !str.includes(s))) {
22+
if (cur_ans === null) {
23+
cur_ans = s
24+
} else if (s.length < cur_ans.length) {
25+
cur_ans = s
26+
} else if (s.length === cur_ans.length && s < cur_ans) {
27+
cur_ans = s
28+
}
29+
}
30+
}
31+
ans.push(cur_ans || '')
32+
}
33+
return ans
34+
};

0 commit comments

Comments
 (0)