Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int longestValidParentheses(string s) {
- stack< pair<char,int> >a;
- int ans=0;
- a.push({'[',0});
- for(auto i:s)
- {
- if(i=='(')a.push({i,0});
- else if(i==')'){
- if(a.size()>1 && a.top().first=='('){
- int cnt=a.top().second+2;
- a.pop();
- pair<char,int> op=a.top();
- a.pop(); op.second+=cnt;
- a.push(op);
- }
- else a.push({i,0});
- }
- }
- while(a.size()){
- ans=max(ans, a.top().second);
- a.pop();
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement