File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments