Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
stack<int> mySTK;
mySTK.push(-1);
int finalRes = 0;
for(int i = 0; i < s.length(); i++){
int t = mySTK.top();
if(t != -1 && s[i] == ')' && s[t] == '('){
mySTK.pop();
finalRes = max(finalRes, i - mySTK.top());
}
else{
mySTK.push(i);
}
}
return finalRes;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment