Given a string containing just the characters’(‘and’)’, find the length of the longest valid (well-formed) parentheses substring.
For”(()”, the longest valid parentheses substring is”()”, which has length = 2.
Another example is”)()())”, where the longest valid parentheses substring is”()()”, which has length = 4.
class Solution {
public:
int longestValidParentheses(string s)
{
stack<int> st;
int start = -1;
int maxl = 0;
for(int i=0; i<s.size(); i++)
{
if(s[i] == '(')
{
st.push(i);
}
else
{
if(!st.empty())
{
st.pop();
if(!st.empty())
maxl = max(maxl, i-st.top());
else
maxl = max(maxl, i-start);
}
else
{
start = i;
}
}
}
return maxl;
}
};