Skip to content

Commit 4b35bf6

Browse files
authored
Merge pull request #684 from karan112005/karan112005-patch-1
Added Solution for LeetCode Problem No. 32
2 parents 5d14dbb + 039601c commit 4b35bf6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/longest-valid-parentheses/
2+
3+
class Solution {
4+
public int longestValidParentheses(String s) {
5+
Stack<Integer> stack = new Stack();
6+
stack.push(-1);
7+
int maxLen = 0;
8+
for(int i = 0; i < s.length(); i++)
9+
{
10+
if(s.charAt(i) == '(')
11+
stack.push(i);
12+
else if(s.charAt(i) == ')')
13+
{
14+
stack.pop();
15+
if(stack.empty())
16+
stack.push(i);
17+
else
18+
maxLen = Math.max(maxLen, i - stack.peek());
19+
}
20+
}
21+
return maxLen;
22+
}
23+
}

0 commit comments

Comments
 (0)