-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Valid Parentheses
34 lines (30 loc) · 1 KB
/
Longest Valid Parentheses
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* given string having only '(' or ')' . find longest valid/balanced substring
Sample Input 1:
2
)()()))
)))(((
Sample Output 1:
4
0
Approach:
simply follow the parantheses balancing procedure. on getting '(' push to stack. And on getting ')' pop from stack. and update the balance count.
for getting balance count at current state, maintain a stack having indices of the '(' so that on getting ')' length can be deduced out.
for preventing early/invalid occurrence of ')' we can push -1 in stack at start. And in intermediate when stack becomes empty, we would push index of ')' too.
That will indicate the finish point of previously balanced substring/group.
#include<stack>
int longestValidParentheses(string &s) {
int ans=0;
stack<int>stk;
stk.push(-1);
for(int i=0;i<s.length();++i){
if(s[i]=='(') stk.push(i);
else{
stk.pop();
if(stk.empty()) stk.push(i);
else ans = max(ans,i-stk.top());
}
}
return ans;
}
//T.C. : O(N)
//S.C. : O(N)