From 208a848c429e966026e0bf6b73117d98d8576a7a Mon Sep 17 00:00:00 2001 From: Mrigesh patni Date: Wed, 12 Oct 2022 17:46:59 +0530 Subject: [PATCH] Create LongestValidParenthesis.py --- .../32.LongestVali/LongestValidParenthesis.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Leetcode-Hard/32.LongestVali/LongestValidParenthesis.py diff --git a/Leetcode-Hard/32.LongestVali/LongestValidParenthesis.py b/Leetcode-Hard/32.LongestVali/LongestValidParenthesis.py new file mode 100644 index 0000000..188a511 --- /dev/null +++ b/Leetcode-Hard/32.LongestVali/LongestValidParenthesis.py @@ -0,0 +1,16 @@ +class Solution: + def longestValidParentheses(self, s: str) -> int: + count=0 + stack = [] + stack.append(-1) + for i in range(len(s)): + if s[i]=='(': + stack.append(i) + else: + stack.pop() + if len(stack)==0: + stack.append(i) + else: + count = max(count,i-stack[-1]) + + return count