Advertisement
raghuvanshraj

32.cpp

Jul 28th, 2021
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  01.10.2019 06:05:23 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. int longestValidParentheses(string s) {
  10.     int n = s.size();
  11.     stack<int> st;
  12.     st.push(-1);
  13.     int res = 0;
  14.     for (int i = 0; i < n; ++i) {
  15.         if (s[i] == '(') {
  16.             st.push(i);
  17.         } else {
  18.             st.pop();
  19.             if (not st.empty()) {
  20.                 res = max(res, i - st.top());
  21.             } else {
  22.                 st.push(i);
  23.             }
  24.         }
  25.     }
  26.  
  27.     return res;
  28. }
  29.  
  30. int main(int argc, char const *argv[]) {
  31.     ios::sync_with_stdio(false);
  32.     cin.tie(0);
  33.  
  34.     string s;
  35.     cin >> s;
  36.     cout << longestValidParentheses(s);
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement