From a5b6351b059639d2059f048f6878e43c50f53859 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Sun, 4 Nov 2018 10:01:34 +0000 Subject: [PATCH] Added Solution.py --- .../032.Longest Valid Parentheses/Solution.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solution/032.Longest Valid Parentheses/Solution.py diff --git a/solution/032.Longest Valid Parentheses/Solution.py b/solution/032.Longest Valid Parentheses/Solution.py new file mode 100644 index 0000000000000..526885d6a2dba --- /dev/null +++ b/solution/032.Longest Valid Parentheses/Solution.py @@ -0,0 +1,24 @@ +class Solution: + def longestValidParentheses(self, s): + """ + :type s: string + :rtype int + """ + + Longest = temp = 0 + stack = [] + + for i in s: + if i == '(' : + stack.append(i) + elif len(stack) != 0 and stack[-1] == '(' : + stack.pop() + temp += 2 + else: + stack=[] + if temp > Longest: + Longest = temp + temp = 0 + if temp > Longest: + Longest = temp + return Longest \ No newline at end of file