class Solution {
public:
int longestValidParentheses(string s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
stack sta;
int i = 0, j = 0, maxV = 0, count = 0;
for (i = 0; i < s.length(); ++i) {
if (s[i] == '(')
sta.push(i);
else {
if (!sta.empty() && s[sta.top()] == '(') {
sta.pop();
if (sta.empty())
count = i + 1;
else
count = i - sta.top();
if (count >
maxV)
maxV = count;
}
else
sta.push(i);
}
}
return maxV;
}
};