File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var longestValidParentheses = function ( s ) {
6
+ let longestValidParenthesesFromIndex = ( index ) => {
7
+ let result = 0 ;
8
+ let count = 0 ;
9
+ let stack = [ ] ;
10
+ restOfString = s . slice ( index ) ;
11
+
12
+ for ( let i = 0 ; i < restOfString . length ; i ++ ) {
13
+ let current = restOfString [ i ] ;
14
+ if ( current === "(" ) {
15
+ count ++ ;
16
+ stack . push ( current ) ;
17
+ } else if ( current === ")" ) {
18
+ if ( stack . length === 0 ) {
19
+ break ;
20
+ }
21
+ count ++ ;
22
+ stack . pop ( ) ;
23
+ if ( stack . length === 0 ) {
24
+ result = count ;
25
+ }
26
+ }
27
+ }
28
+
29
+ return result ;
30
+ } ;
31
+
32
+ let result = 0 ;
33
+
34
+ for ( let i = 0 ; i < s . length ; i ++ ) {
35
+ let indexResult = longestValidParenthesesFromIndex ( i ) ;
36
+
37
+ if ( indexResult > result ) {
38
+ result = indexResult ;
39
+ }
40
+ }
41
+
42
+ return result ;
43
+ } ;
44
+
45
+ console . log ( longestValidParentheses ( ")()())" ) ) ;
You can’t perform that action at this time.
0 commit comments