Skip to content

Commit 4c819ee

Browse files
committed
fix: update ValidParentheses with Deque and improved tests
1 parent f56c249 commit 4c819ee

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/main/java/com/thealgorithms/stacks/ValidParentheses.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
package com.thealgorithms.stacks;
22

3-
import java.util.Stack;
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.Map;
46

57
/**
6-
* Utility class to check for valid parentheses in a string.
8+
* Utility class to check if a string has valid parentheses.
79
*/
810
public final class ValidParentheses {
911

1012
private ValidParentheses() {
1113
throw new AssertionError("Cannot instantiate utility class");
1214
}
1315

16+
private static final Map<Character, Character> PAIRS = Map.of(
17+
')', '(', '}', '{', ']', '['
18+
);
19+
1420
/**
15-
* Returns true if the input string has valid parentheses.
21+
* Checks if the input string has valid parentheses.
1622
*
17-
* @param s the string containing parentheses
23+
* @param s the input string
1824
* @return true if valid, false otherwise
1925
*/
2026
public static boolean isValid(final String s) {
21-
final Stack<Character> stack = new Stack<>();
27+
if (s == null) {
28+
throw new NullPointerException("Input cannot be null");
29+
}
2230

23-
for (final char ch : s.toCharArray()) {
24-
if (ch == '(' || ch == '{' || ch == '[') {
31+
Deque<Character> stack = new ArrayDeque<>();
32+
for (char ch : s.toCharArray()) {
33+
if (PAIRS.containsValue(ch)) { // opening bracket
2534
stack.push(ch);
26-
} else if (ch == ')' || ch == '}' || ch == ']') {
27-
if (stack.isEmpty()) {
28-
return false;
29-
}
30-
final char top = stack.peek();
31-
if ((top == '(' && ch == ')')
32-
|| (top == '{' && ch == '}')
33-
|| (top == '[' && ch == ']')) {
34-
stack.pop();
35-
} else {
35+
} else if (PAIRS.containsKey(ch)) { // closing bracket
36+
if (stack.isEmpty() || stack.pop() != PAIRS.get(ch)) {
3637
return false;
3738
}
3839
}
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
package com.thealgorithms.stacks;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import java.util.stream.Stream;
66
import org.junit.jupiter.params.ParameterizedTest;
7-
import org.junit.jupiter.params.provider.Arguments;
8-
import org.junit.jupiter.params.provider.MethodSource;
7+
import org.junit.jupiter.params.provider.CsvSource;
98

109
/**
1110
* Tests for {@link ValidParentheses}.
1211
*/
1312
public class ValidParenthesesTest {
1413

1514
@ParameterizedTest
16-
@MethodSource("provideValidTestCases")
17-
void testIsValidValidCases(String input, Boolean expected) {
15+
@CsvSource({
16+
"'()', true",
17+
"'()[]{}', true",
18+
"'{[]}', true",
19+
"'', true"
20+
})
21+
void testValidParentheses(String input, boolean expected) {
1822
assertEquals(expected, ValidParentheses.isValid(input));
1923
}
2024

21-
static Stream<Arguments> provideValidTestCases() {
22-
return Stream.of(
23-
Arguments.of("()", Boolean.TRUE),
24-
Arguments.of("()[]{}", Boolean.TRUE),
25-
Arguments.of("{[]}", Boolean.TRUE),
26-
Arguments.of("", Boolean.TRUE) // empty string is valid
27-
);
28-
}
29-
3025
@ParameterizedTest
31-
@MethodSource("provideInvalidTestCases")
32-
void testIsValidInvalidCases(String input, Boolean expected) {
26+
@CsvSource({
27+
"'(', false",
28+
"')', false",
29+
"'([)]', false",
30+
"'{[}]', false",
31+
"'((()', false"
32+
})
33+
void testInvalidParentheses(String input, boolean expected) {
3334
assertEquals(expected, ValidParentheses.isValid(input));
3435
}
3536

36-
static Stream<Arguments> provideInvalidTestCases() {
37-
return Stream.of(
38-
Arguments.of("(", Boolean.FALSE),
39-
Arguments.of(")", Boolean.FALSE),
40-
Arguments.of("([)]", Boolean.FALSE),
41-
Arguments.of("{[}]", Boolean.FALSE),
42-
Arguments.of("((()", Boolean.FALSE)
43-
);
37+
@ParameterizedTest
38+
@CsvSource({
39+
"null"
40+
})
41+
void testNullInput(String input) {
42+
assertThrows(NullPointerException.class, () -> ValidParentheses.isValid(null));
4443
}
4544
}

0 commit comments

Comments
 (0)