-
Notifications
You must be signed in to change notification settings - Fork 818
/
LongestValidParentheses.java
69 lines (62 loc) · 1.86 KB
/
LongestValidParentheses.java
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
package stack;
import java.util.*;
/**
* Created by gouthamvidyapradhan on 08/07/2018. Given a string containing just the characters '('
* and ')', find the length of the longest valid (well-formed) parentheses substring.
*
* <p>Example 1:
*
* <p>Input: "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" Example 2:
*
* <p>Input: ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()"
*
* <p>Solution: O(N) Iterate through each of the parentheses and if '(' is encountered push it to
* stack else check the top of the stack to see if there is a matching parentheses, if yes pop it
* and then take the length (currIndex - index at top of the stack). Maintain a max length and
* return this as the answer.
*/
public class LongestValidParentheses {
private class Node {
char c;
int i;
Node(char c, int i) {
this.c = c;
this.i = i;
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new LongestValidParentheses().longestValidParentheses("((()()(((())))))"));
}
public int longestValidParentheses(String s) {
Stack<Node> stack = new Stack<>();
int max = 0;
for (int i = 0, l = s.length(); i < l; i++) {
char c = s.charAt(i);
switch (c) {
case '(':
stack.push(new Node(c, i));
break;
case ')':
if (!stack.isEmpty()) {
if (stack.peek().c == '(') {
stack.pop();
if (stack.isEmpty()) {
max = Math.max(max, i + 1);
} else {
max = Math.max(max, i - stack.peek().i);
}
} else {
stack.push(new Node(c, i));
}
} else {
stack.push(new Node(c, i));
}
}
}
return max;
}
}