Skip to content

Commit ee57b78

Browse files
committed
LeetCode 20: Valid Parentheses (remastered)
1 parent d197df6 commit ee57b78

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/main/java/org/gd/leetcode/p0020/Solution.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @see org.gd.leetcode.p0022.Solution
1010
* @since 2018-12-29
1111
*/
12+
@SuppressWarnings("JavadocReference")
1213
@LeetCode(
1314
difficulty = LeetCode.Level.EASY,
1415
tags = {
@@ -17,22 +18,28 @@
1718
})
1819
class Solution {
1920

20-
private static int map(int c) {
21-
switch (c) {
22-
case ')': return '(';
23-
case ']': return '[';
24-
case '}': return '{';
25-
}
26-
return -1;
21+
private final byte[] hashTable;
22+
23+
public Solution() {
24+
hashTable = new byte[Byte.MAX_VALUE];
25+
hashTable[')'] = '(';
26+
hashTable[']'] = '[';
27+
hashTable['}'] = '{';
2728
}
2829

2930
public boolean isValid(String s) {
3031

32+
if (s == null || s.length() % 2 != 0) {
33+
return false;
34+
}
35+
3136
int[] stack = new int[s.length()];
3237

3338
int c, mapped, head = 0;
3439
for (int i = 0; i < s.length(); i++) {
35-
if ((mapped = map(c = s.charAt(i))) > 0) {
40+
c = s.charAt(i);
41+
mapped = hashTable[c];
42+
if (mapped > 0) {
3643
int top = head <= 0 ? -2 : stack[--head];
3744
if (top != mapped)
3845
return false;

src/main/java/org/gd/leetcode/p0022/Solution.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
11
package org.gd.leetcode.p0022;
22

3+
import org.gd.leetcode.common.LeetCode;
4+
import org.gd.leetcode.common.LeetCode.Level;
5+
import org.gd.leetcode.common.LeetCode.Tags;
6+
37
import java.nio.charset.StandardCharsets;
48
import java.util.ArrayList;
59
import java.util.Arrays;
610
import java.util.Collections;
711
import java.util.List;
812

9-
import org.gd.leetcode.common.LeetCode;
10-
import org.gd.leetcode.common.LeetCode.Level;
11-
import org.gd.leetcode.common.LeetCode.Tags;
12-
1313
/**
1414
* https://leetcode.com/problems/generate-parentheses/
15-
*
15+
*
1616
* Given n pairs of parentheses, write a function to generate all combinations
1717
* of well-formed parentheses.
18-
*
18+
*
1919
* For example, given n = 3, a solution set is:
20-
*
20+
*
2121
* [
2222
* "((()))",
2323
* "(()())",
2424
* "(())()",
2525
* "()(())",
2626
* "()()()"
2727
* ]
28-
*
28+
*
2929
* @author Horkhover Dmytro
3030
* @see org.gd.leetcode.p0020.Solution
3131
*/
32-
@LeetCode(difficulty = Level.MEDIUM, tags = { Tags.BACKTRACKING, Tags.STRING })
32+
@SuppressWarnings("JavadocReference")
33+
@LeetCode(
34+
difficulty = Level.MEDIUM,
35+
tags = {
36+
Tags.BACKTRACKING,
37+
Tags.STRING
38+
})
3339
class Solution {
3440

3541
private List<String> list;
3642

3743
public List<String> generateParenthesis(int n) {
38-
if (n <= 0)
44+
if (n <= 0) {
3945
return Collections.emptyList();
46+
}
4047

41-
if (n == 1)
48+
if (n == 1) {
4249
return Arrays.asList("()");
50+
}
4351

4452
list = new ArrayList<>();
4553
byte[] chars = new byte[n << 1];

src/test/java/org/gd/leetcode/p0020/SolutionTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.gd.leetcode.p0020;
22

3-
import org.junit.jupiter.api.*;
3+
import lombok.NonNull;
4+
import org.junit.jupiter.api.DisplayName;
45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.Arguments;
67
import org.junit.jupiter.params.provider.MethodSource;
78

89
import java.util.stream.Stream;
910

10-
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
1112

1213
/**
1314
* Test for {@link Solution};
@@ -17,11 +18,14 @@
1718
*/
1819
class SolutionTest {
1920

21+
@NonNull
2022
private static Stream<Arguments> args() {
23+
String longTestCase0 = "[([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([()])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])]";
2124
return Stream.of(
2225
Arguments.of("()[]{", false),
2326
Arguments.of("{[]}", true),
24-
Arguments.of("([)]", false)
27+
Arguments.of("([)]", false),
28+
Arguments.of(longTestCase0, true)
2529
);
2630
}
2731

0 commit comments

Comments
 (0)