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