We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 51dba4d commit b9c7a17Copy full SHA for b9c7a17
strings/longest_valid_paranthesis.py
@@ -0,0 +1,32 @@
1
+def longest_valid_paranthesis(self, s: str) -> int:
2
+ """
3
+ Returns the length of the longest valid paranthesis
4
+ >>> longest_valid_paranthesis('(()')
5
+ 2
6
+ >>> longest_valid_paranthesis(')()())')
7
+ 4
8
+ >>> longest_valid_paranthesis('')
9
+ 0
10
+ >>> longest_valid_paranthesis(''(())))((()(()()()())
11
+ 8
12
13
+ stack = []
14
+ preceeding_matched = 0
15
+ res = 0
16
+ for char in s:
17
+ if char == "(":
18
+ stack.append(preceeding_matched)
19
20
+ else:
21
+ if stack:
22
+ preceeding_matched += 1 + stack.pop()
23
24
+ res = max(res, preceeding_matched)
25
26
+
27
28
29
+ while stack:
30
+ res = max(res, stack.pop())
31
32
+ return res * 2
0 commit comments