File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {string }
4
+ */
5
+ const longestNiceSubstring = function ( s ) {
6
+ let res = ''
7
+ const n = s . length
8
+
9
+ const arr = Array ( 26 ) . fill ( null )
10
+ for ( let i = 0 ; i < n - 1 ; i ++ ) {
11
+ for ( let j = i + 1 ; j < n ; j ++ ) {
12
+ const tmp = s . slice ( i , j + 1 )
13
+ if ( helper ( tmp ) ) {
14
+ if ( tmp . length > res . length ) res = tmp
15
+ }
16
+ }
17
+ }
18
+
19
+
20
+ return res
21
+ } ;
22
+
23
+ function helper ( s ) {
24
+ const arr = Array ( 26 ) . fill ( null )
25
+ const a = 'a' . charCodeAt ( 0 ) , A = 'A' . charCodeAt ( 0 )
26
+ for ( let e of s ) {
27
+ const ecode = e . charCodeAt ( 0 )
28
+ if ( arr [ ecode - a ] === 0 || arr [ ecode - A ] === 0 ) continue
29
+ if ( ecode - a < 26 && ecode - a >= 0 ) arr [ ecode - a ] = arr [ ecode - a ] === 1 ? 0 : - 1
30
+ if ( ecode - A < 26 && ecode - A >= 0 ) arr [ ecode - A ] = arr [ ecode - A ] === - 1 ? 0 : 1
31
+ }
32
+ for ( let e of arr ) {
33
+ if ( e === - 1 || e === 1 ) return false
34
+ }
35
+
36
+ return true
37
+ }
You can’t perform that action at this time.
0 commit comments