-
Notifications
You must be signed in to change notification settings - Fork 0
/
100.StackLongestValidSubstring.cpp
77 lines (69 loc) · 1.88 KB
/
100.StackLongestValidSubstring.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Approach 1 : Using Stack - Push -1 into stack as starting index. Push index of '(' into stack if encountered. If there is no '(' and
// ')' is encountered then change starting index from -1 to index of ')'. If a pair is matched then pop and length of valid
// substring is i - s.top(). Compare it with the global longest length.
// Approach 2 : Using two variables left and right which keep track of number of '(' and ')'. If number of ')' is greater than number
// of '(' then reset left = right = 0 else if both are equal then length of valid string = 2 * left. We need check from both
// side of array to check for cases such as (() where left will always be larger than right.
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
// ------>>> Approach 1 <<<-------
// int findMaxLen(string s) {
// stack<int>st;
// int counter = -1;
// st.push(counter);
// int result = 0;
// for(int i = 0; i < s.size(); i++) {
// if(s[i] == '(') {
// st.push(i);
// }
// else {
// if(st.top() == counter) {
// st.pop();
// counter = i;
// st.push(counter);
// }
// else{
// st.pop();
// int temp = st.top();
// int tempResult = i - temp;
// result = max(result, tempResult);
// }
// }
// }
// return result;
// }
// ------>>> Approach 2 <<<-------
int findMaxLen(string s) {
int left = 0, right = 0;
int result = 0;
for(int i = 0; i < s.size(); i++) {
s[i] == '(' ? left++ : right++;
if(left == right) {
result = max(result, right * 2);
}
if(right > left) {
left = 0;
right = 0;
}
}
left = 0;
right = 0;
for(int i = s.size() - 1; i >= 0; i--) {
s[i] == ')' ? left++ : right++;
if(left == right) {
result = max(result, right * 2);
}
if(right > left) {
left = 0;
right = 0;
}
}
return result;
}
int main() {
string A = "((()()()()(((())";
cout << findMaxLen(A);
return 0;
}