Stack
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
| class Solution { public int longestValidParentheses(String s) { Stack<Integer> leftBracketIndexStack = new Stack<>();
int maxLength = 0; int startIndex = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { leftBracketIndexStack.push(i); } else { if (leftBracketIndexStack.isEmpty()) { startIndex = i + 1; } else { leftBracketIndexStack.pop();
if (leftBracketIndexStack.isEmpty()) { maxLength = Math.max(maxLength, i - startIndex + 1); } else { maxLength = Math.max(maxLength, i - leftBracketIndexStack.peek()); } } } }
return maxLength; } }
|
DP(TLE)
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
| class Solution { public int longestValidParentheses(String s) { int n = s.length();
int maxLength = 0;
boolean[][] dp = new boolean[n][n]; for (int i = n - 1; i >= 0; i--) { char l = s.charAt(i); for (int j = i + 1; j < n; j++) { char r = s.charAt(j);
if (l == '(' && r == ')') { if (j == i + 1) { dp[i][j] = true; } else { dp[i][j] = dp[i + 1][j - 1]; }
for (int leftEndIndex = i + 1; leftEndIndex < j - 1 && !dp[i][j]; leftEndIndex++) { dp[i][j] = dp[i][leftEndIndex] && dp[leftEndIndex + 1][j]; }
if (dp[i][j]) { maxLength = Math.max(maxLength, j - i + 1); } } } }
return maxLength; } }
|
DP
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
| class Solution { public int longestValidParentheses(String s) { int n = s.length(); int maxLength = 0;
int[] dp = new int[n + 1];
for (int i = 1; i <= s.length(); i++) { char c = s.charAt(i - 1); if (c == ')') {
if (i - 2 >= 0) { char prev = s.charAt(i - 2); if (prev == '(') { dp[i] = dp[i - 2] + 2; } else { int subStrLength = dp[i - 1]; if (subStrLength > 0) { int leftmostIndex = i - 1 - subStrLength - 1; if (leftmostIndex >= 0 && s.charAt(leftmostIndex) == '(') { dp[i] = dp[leftmostIndex] + 1 + dp[i - 1] + 1; } } }
maxLength = Math.max(maxLength, dp[i]); } } }
return maxLength; } }
|
References
32. Longest Valid Parentheses