class Solution {
public:
int longestValidParentheses(string str) {
int window = 0;
stack<char> ss;
stack<int> num;
num.push(-1);
int size = str.size();
for (int i =0; i < size; i++) {
if (str[i] == '(') {
ss.push('(');
num.push(i);
} else {
if (ss.empty() || ss.top() == ')'){
ss.push(')');
num.push(i);
} else {
ss.pop();
num.pop();
window = max(window, i - num.top());
}
}
}
return window;
}
};