-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Leetcode 32. Longest Valid Parentheses
- Loading branch information
1 parent
90f4161
commit 4ba98f2
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package leetcode.LC_032; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* 32. Longest Valid Parentheses | ||
*/ | ||
class Solution32 { | ||
/* | ||
* | ||
* 0 1 2 3 4 5 6 | ||
* 3 | ||
* 2 2 2 | ||
* 1 1 1 1 1 | ||
* ) ( ( ( ) ) ) | ||
* S maxLen: 0 | ||
* 2 4 maxLen: 4 - 2 = 2 | ||
* 1 5 maxLen: max(2, 5 - 1) = 4 | ||
* S 6 maxLen: max(4, 6 - S) = 6 | ||
* | ||
*/ | ||
int longestValidParentheses(String s) { | ||
int maxLen = 0; | ||
int curStart = -1; // We need this `curStart` because open left parentheses keep being consumed in a very long sequence, | ||
// such as: "(()()()()()()()())", where `curStart` is first `-1` and then always `0`. | ||
|
||
// A stack to memorize indices of previous, i.e. OPEN, left parentheses | ||
Stack<Integer> lefts = new Stack<>(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
if (s.charAt(i) == '(') { | ||
lefts.push(i); | ||
|
||
} else { | ||
if (lefts.isEmpty()) { | ||
curStart = i; // This resets the start index for current sequence. | ||
|
||
} else { | ||
lefts.pop(); // Consumes the last open left parenthesis, and decide the length of current valid sequence: | ||
// If stack empty: `curStart`, else: the last open left parenthesis. | ||
final int prevStart = lefts.isEmpty() ? curStart : lefts.peek(); | ||
maxLen = Math.max(maxLen, i - prevStart); // Note that both `curStart` and `lefts.peek()` is the index JUST BEFORE current valid sequence. | ||
} | ||
} | ||
} | ||
return maxLen; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package leetcode.LC_032; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class Solution32Test { | ||
|
||
@Test | ||
public void longestValidParentheses_l_l_r() { | ||
assertEquals(2, new Solution32().longestValidParentheses("(()")); | ||
} | ||
|
||
@Test | ||
public void longestValidParentheses_r_l_r_l_r_r() { | ||
assertEquals(4, new Solution32().longestValidParentheses(")()())")); | ||
} | ||
|
||
@Test | ||
public void longestValidParentheses_r_l_l_l_r_r_r_l() { | ||
assertEquals(6, new Solution32().longestValidParentheses(")((()))(")); | ||
} | ||
|
||
} |