From 1552f312ba18f37ec914e84fb8f11e990c21958f Mon Sep 17 00:00:00 2001 From: Mrigesh901 Date: Tue, 11 Oct 2022 15:18:20 +0530 Subject: [PATCH] Create 32.LongestValidParenthesis --- Leetcode-Hard/32.LongestValidParenthesis | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Leetcode-Hard/32.LongestValidParenthesis diff --git a/Leetcode-Hard/32.LongestValidParenthesis b/Leetcode-Hard/32.LongestValidParenthesis new file mode 100644 index 0000000..9aa389f --- /dev/null +++ b/Leetcode-Hard/32.LongestValidParenthesis @@ -0,0 +1,17 @@ +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 +