Skip to content

Commit 76c4ee8

Browse files
authored
32. Longest Valid Parentheses
1 parent d8084c7 commit 76c4ee8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

32. Longest Valid Parentheses

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//simple c++ approach
2+
//T.C- O(n)
3+
//Stack class 3
4+
//HARD question
5+
6+
class Solution {
7+
public:
8+
int longestValidParentheses(string s) {
9+
stack<int>st;
10+
st.push(-1);
11+
int maxlen =0;
12+
for(int i=0;i<s.length();i++){
13+
char ch = s[i];
14+
if(ch == '('){
15+
st.push(i);
16+
}
17+
else{
18+
st.pop();
19+
if(st.empty()) {
20+
st.push(i); //hard area
21+
}
22+
else{
23+
int len = i - st.top();
24+
maxlen = max(len,maxlen);
25+
}
26+
}
27+
}
28+
return maxlen;
29+
}
30+
};

0 commit comments

Comments
 (0)