Skip to content

Commit b7d25c6

Browse files
committed
added LongestValidParentheses solution
1 parent 9943aaa commit b7d25c6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ The platform currently supports a total of 11 languages: C, C++, Java, Python, C
120120
- 19 RemoveNthNodeFromEndOfList (RemoveNthNodeFromEndOfListTest) - linked list remove
121121
- 20 ValidParentheses same as BalancedParentheses (BalancedParenthesesTest)
122122
- 22 GenerateParentheses (GenerateParenthesesTest) - generate all valid combinations of n parentheses
123+
- 32 LongestValidParentheses (LongestValidParenthesesTest) - find longest substring of valid parentheses
123124
- 42 TrappingRainWater (TrappingRainWaterTest) - calculate water inside the landscape
124125
- 44 WildcardMatching (WildcardMatchingTest) - checking wildcard pattern matching
125126
- 45 JumpGame2 (JumpGame2Test) - Jump Game / Tower Hopper problem / Minimum number of jumps to reach the high based on possible range to jump from current element.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package iurii.job.interview.leetcode;
2+
3+
/**
4+
* 32. Longest Valid Parentheses https://leetcode.com/problems/longest-valid-parentheses/description/
5+
*
6+
* Ideas are here https://leetcode.com/articles/longest-valid-parentheses/
7+
*
8+
* Brute force, dynamic programming, stack and two pointers
9+
*
10+
* Time complexity: O(N)
11+
* Auxiliary space complexity: O(N) with stack and O(1) with two pointers
12+
*/
13+
public class LongestValidParentheses {
14+
15+
public int longestValidParentheses(String s) {
16+
int left = 0, right = 0, maxLength = 0;
17+
for (int i = 0; i < s.length(); i++) {
18+
if (s.charAt(i) == '(') {
19+
left++;
20+
} else {
21+
right++;
22+
}
23+
if (left == right) {
24+
maxLength = Math.max(maxLength, left + right);
25+
} else if (right > left) {
26+
left = right = 0;
27+
}
28+
}
29+
left = right = 0;
30+
for (int i = s.length() - 1; i >= 0; i--) {
31+
if (s.charAt(i) == '(') {
32+
left++;
33+
} else {
34+
right++;
35+
}
36+
if (left == right) {
37+
maxLength = Math.max(maxLength, left + right);
38+
} else if (left > right) {
39+
left = right = 0;
40+
}
41+
}
42+
return maxLength;
43+
}
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package iurii.job.interview.leetcode;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class LongestValidParenthesesTest {
8+
@Test
9+
public void test() {
10+
LongestValidParentheses longestValidParentheses = new LongestValidParentheses();
11+
assertThat(longestValidParentheses.longestValidParentheses(")()())")).isEqualTo(4);
12+
assertThat(longestValidParentheses.longestValidParentheses("()")).isEqualTo(2);
13+
assertThat(longestValidParentheses.longestValidParentheses(")))(((")).isEqualTo(0);
14+
}
15+
}

0 commit comments

Comments
 (0)