Skip to content
37 changes: 37 additions & 0 deletions src/main/java/com/thealgorithms/stacks/ValidParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.stacks;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;

public final class ValidParentheses {

private ValidParentheses() {
throw new AssertionError("Cannot instantiate utility class");
}

private static final Map<Character, Character> PAIRS = Map.of(')', '(', '}', '{', ']', '[');

public static boolean isValid(final String s) {
if (s == null) {
throw new NullPointerException("Input cannot be null");
}

Deque<Character> stack = new ArrayDeque<>();
for (char ch : s.toCharArray()) {
if (PAIRS.containsValue(ch)) { // opening bracket
stack.push(ch);
} else if (PAIRS.containsKey(ch)) { // closing bracket
// Split logic to satisfy PMD
if (stack.isEmpty()) {
return false;
}
Character top = stack.pop();
if (top != PAIRS.get(ch)) {
return false;
}
}
}
return stack.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.thealgorithms.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Tests for {@link ValidParentheses}.
*/
public class ValidParenthesesTest {

@ParameterizedTest
@CsvSource({"'()', true", "'()[]{}', true", "'{[]}', true", "'', true"})
void testValidParentheses(String input, boolean expected) {
assertEquals(expected, ValidParentheses.isValid(input));
}

@ParameterizedTest
@CsvSource({"'(', false", "')', false", "'([)]', false", "'{[}]', false", "'((()', false"})
void testInvalidParentheses(String input, boolean expected) {
assertEquals(expected, ValidParentheses.isValid(input));
}

@ParameterizedTest
@CsvSource({"null"})
void testNullInput(String input) {
assertThrows(NullPointerException.class, () -> ValidParentheses.isValid(null));
}
}