Longest Valid Parentheses
Hard · String, Dynamic Programming, Stack
Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.
Example 1
Input: s = "(()" Output: 2
The longest valid parentheses substring is "()".
Example 2
Input: s = ")()())" Output: 4
The longest valid parentheses substring is "()()".
Example 3
Input: s = "" Output: 0
Constraints
- 0 <= s.length <= 3 * 104
- s[i] is '(', or ')'.
Starter code (python)
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Roman to Integer (Easy)
Previous: Remove Element · Next: Search in Rotated Sorted Array · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.