File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } A
3
+ * @return {string[] }
4
+ */
5
+ const commonChars = function ( A ) {
6
+ const minArr = minEl ( A )
7
+ const res = [ ]
8
+ for ( let i = 0 ; i < minArr [ 1 ] ; i ++ ) {
9
+ let target = A [ minArr [ 0 ] ] [ i ]
10
+ let all = true
11
+ for ( let j = 0 ; j < A . length ; j ++ ) {
12
+ if ( j === minArr [ 0 ] ) continue
13
+ if ( all === false ) continue
14
+ let idx
15
+ if ( ( idx = A [ j ] . indexOf ( target ) ) === - 1 ) {
16
+ all = false
17
+ } else {
18
+ A [ j ] = A [ j ] . slice ( 0 , idx ) + A [ j ] . slice ( idx + 1 )
19
+ }
20
+ }
21
+ if ( all ) res . push ( target )
22
+ }
23
+
24
+ return res
25
+ } ;
26
+
27
+ function minEl ( arr ) {
28
+ const res = [ 0 , Number . MAX_SAFE_INTEGER ] // [idx, len]
29
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
30
+ if ( arr [ i ] . length < res [ 1 ] ) {
31
+ res [ 0 ] = i
32
+ res [ 1 ] = arr [ i ] . length
33
+ }
34
+ }
35
+ return res
36
+ }
You can’t perform that action at this time.
0 commit comments