forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestValidParenthesisSubstring.java
More file actions
76 lines (66 loc) · 2.45 KB
/
LongestValidParenthesisSubstring.java
File metadata and controls
76 lines (66 loc) · 2.45 KB
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
package com.geeksforgeeks.stack;
import com.util.LogUtil;
import java.util.Stack;
/**
* https://leetcode.com/problems/longest-valid-parentheses/
* <p>
* 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 "()()"
*
* @author neeraj on 2019-07-20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class LongestValidParenthesisSubstring {
public static void main(String[] args) {
findLongestValidParenthesis("((()(()((()))");
findLongestValidParenthesis("((()");
findLongestValidParenthesis(")()())");
findLongestValidParenthesis("()(()))))");
findLongestValidParenthesis("()(()))())()");
}
public static void findLongestValidParenthesis(String str) {
char[] arr = str.toCharArray();
Stack<Integer> stack = new Stack<>();
int MAX_LENGTH = 0;
/**
* For the base case we need (-1) to be the first top element in the Stack
*
* Reason : Suppose we have a String = ();
* Now when "(" comes we push it's index to the Stack
* and when ")" comes, we just pop out the closest "(" bracket index from Stack.
*
* Now after this pop operation we need to evaluate that how much this pair contributed to the final output
* this can be done by subtracting the currentIndex - whateverIndexWasThereOnTop after popping out the recent, one.
*
* hence pushing (-1) so that for ( )
* after popping, total = 1(currentIndex) - (-1) (Index on Top) = 2;
*
*/
stack.push(-1);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '(') {
stack.push(i); // Pushing the index instead of "(" since it helps to calculate max length
} else {
stack.pop();
// If Stack is not empty
if (!stack.isEmpty()) {
MAX_LENGTH = Math.max(MAX_LENGTH, i - stack.peek());
} else {
stack.push(i);
}
}
}
LogUtil.logIt("Longest Valid Parenthesis Substring in " + str + " is of length " + MAX_LENGTH);
}
}