We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8a220db commit 303aff9Copy full SHA for 303aff9
1 file changed
LONGEST VALID PARENTHESIS_DP
@@ -0,0 +1,20 @@
1
+def longestValidParentheses(A):
2
+ dp = [0 for x in A]
3
+ for i in range(len(A)):
4
+ if A[i] == '(':
5
+ pass
6
+ if A[i] == ')':
7
+ if i-1<0:
8
+ continue
9
+ if A[i-1] == '(':
10
+ dp[i]=dp[i-2]+2
11
12
+ if i-dp[i-1]-1<0:
13
14
+ if A[i-dp[i-1]-1] =='(':
15
+ dp[i] = dp[i-1]+2 + dp[i-dp[i-1]-2]
16
+ if len(dp)==0:
17
+ return 0
18
+ return max(dp)
19
+
20
+print(longestValidParentheses("(())()"))
0 commit comments