Advertisement
unknown_0711

Untitled

Apr 30th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int longestValidParentheses(string s) {
  4. stack< pair<char,int> >a;
  5. int ans=0;
  6. a.push({'[',0});
  7. for(auto i:s)
  8. {
  9. if(i=='(')a.push({i,0});
  10. else if(i==')'){
  11. if(a.size()>1 && a.top().first=='('){
  12. int cnt=a.top().second+2;
  13. a.pop();
  14. pair<char,int> op=a.top();
  15. a.pop(); op.second+=cnt;
  16. a.push(op);
  17.  
  18. }
  19. else a.push({i,0});
  20. }
  21.  
  22. }
  23. while(a.size()){
  24. ans=max(ans, a.top().second);
  25. a.pop();
  26. }
  27. return ans;
  28. }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement