Skip to content

Commit 25c585f

Browse files
Merge pull request jitendrajat10099#20 from vrakr19/add-gfg-sol
add stack problem solution
2 parents 39d5907 + 38a0be6 commit 25c585f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Problem Link: https://practice.geeksforgeeks.org/problems/longest-valid-parentheses/0
3+
*/
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
int main(){
9+
int t;
10+
cin>>t;
11+
while(t--){
12+
string str;
13+
cin>>str;
14+
15+
stack<int> s;
16+
s.push(-1);
17+
int res=0;
18+
for(int i=0;i<str.length();i++){
19+
if(str[i]=='('){
20+
s.push(i);
21+
}else{
22+
s.pop();
23+
if(s.empty()){
24+
s.push(i);
25+
}else{
26+
res = max(res,i-s.top());
27+
}
28+
}
29+
}
30+
cout<<res<<endl;
31+
}
32+
}

0 commit comments

Comments
 (0)