From 3b222e003fbab045101f94d08c759d650e8683b9 Mon Sep 17 00:00:00 2001 From: Mrigesh901 Date: Mon, 10 Oct 2022 19:51:29 +0530 Subject: [PATCH] Create 32. Longest Valid Parenthesis Solved using python --- Leetcode-Hard/32. Longest Valid Parenthesis | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Leetcode-Hard/32. Longest Valid Parenthesis diff --git a/Leetcode-Hard/32. Longest Valid Parenthesis b/Leetcode-Hard/32. Longest Valid Parenthesis new file mode 100644 index 0000000..3d5f338 --- /dev/null +++ b/Leetcode-Hard/32. Longest Valid Parenthesis @@ -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