Longest Valid Parentheses
Given a string s containing only the characters '(' and ')', return the length of the longest contiguous substring that is a well-formed (valid) parenthesis sequence.
Open official problem prompt ↗Measure the longest window of the string that is a balanced parenthesis expression.
Marking the floor just behind you as a reference line; each time you close a matching pair you measure from your current spot back to the last reference line to see how long the balanced stretch is.
- Input
- s = ")()())"
- Output
- 4
- Why
- The substring "()()" from index 1 to 4 is valid and has length 4.
0 <= s.length <= 3 * 10^4s[i] is '(' or ')'