class Solution {
public:
int longestValidParentheses(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int n=s.size();
vector<int> a(n,0);
stack<int> st;
int maxlen=0;
for(int i=0;i<n;i++)
{
if(s[i]=='(')
st.push(i);
else if(s[i]==')')
{
if(!st.empty())
{
a[i]=a[st.top()]=1;
st.pop();
}
}
}
int count=0;
for(int i=0;i<n;i++)
{
if(a[i]==1)
{
count++;
maxlen=max(maxlen,count);
}else
{
count=0;
}
}
return maxlen;
}
};
public:
int longestValidParentheses(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int n=s.size();
vector<int> a(n,0);
stack<int> st;
int maxlen=0;
for(int i=0;i<n;i++)
{
if(s[i]=='(')
st.push(i);
else if(s[i]==')')
{
if(!st.empty())
{
a[i]=a[st.top()]=1;
st.pop();
}
}
}
int count=0;
for(int i=0;i<n;i++)
{
if(a[i]==1)
{
count++;
maxlen=max(maxlen,count);
}else
{
count=0;
}
}
return maxlen;
}
};