Skip to content

Commit ad030d1

Browse files
committed
feat: Add ValidParentheses utility with tests
1 parent f0fb971 commit ad030d1

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Utility class for checking if a given string of parentheses is valid.
7+
* <p>
8+
* A string containing just the characters '(', ')', '{', '}', '[' and ']' is valid if:
9+
* <ul>
10+
* <li>Open brackets are closed by the same type of brackets.</li>
11+
* <li>Open brackets are closed in the correct order.</li>
12+
* </ul>
13+
* <p>
14+
* Example:
15+
* <pre>
16+
* Input: "()[]{}"
17+
* Output: true
18+
* </pre>
19+
* <p>
20+
* For more details, see
21+
* <a href="https://leetcode.com/problems/valid-parentheses/">LeetCode: Valid Parentheses</a>.
22+
* </p>
23+
*/
24+
public final class ValidParentheses {
25+
26+
private ValidParentheses() {
27+
// prevent instantiation
28+
}
29+
30+
/**
31+
* Checks whether the input string has valid parentheses.
32+
*
33+
* @param str input string containing parentheses
34+
* @return true if the string is valid, false otherwise
35+
*/
36+
public static boolean isValid(final String str) {
37+
Stack<Character> stack = new Stack<>();
38+
39+
for (char ch : str.toCharArray()) {
40+
if (ch == '(' || ch == '{' || ch == '[') {
41+
stack.push(ch);
42+
} else {
43+
if (stack.isEmpty()) {
44+
return false;
45+
}
46+
char top = stack.peek();
47+
if ((top == '(' && ch == ')') ||
48+
(top == '{' && ch == '}') ||
49+
(top == '[' && ch == ']')) {
50+
stack.pop();
51+
} else {
52+
return false;
53+
}
54+
}
55+
}
56+
return stack.isEmpty();
57+
}
58+
59+
/**
60+
* Example usage.
61+
*/
62+
public static void main(final String[] args) {
63+
System.out.println(isValid("()[]{}")); // true
64+
System.out.println(isValid("(]")); // false
65+
System.out.println(isValid("([)]")); // false
66+
System.out.println(isValid("{[]}")); // true
67+
}
68+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.stacks;
2+
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class ValidParenthesesTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideValidTestCases")
16+
void testIsValid_ValidCases(String input, boolean expected) {
17+
assertEquals(expected, ValidParentheses.isValid(input));
18+
}
19+
20+
static Stream<Arguments> provideValidTestCases() {
21+
return Stream.of(
22+
Arguments.of("()", true),
23+
Arguments.of("()[]{}", true),
24+
Arguments.of("{[]}", true),
25+
Arguments.of("", true) // empty string is valid
26+
);
27+
}
28+
29+
@ParameterizedTest
30+
@MethodSource("provideInvalidTestCases")
31+
void testIsValid_InvalidCases(String input) {
32+
assertEquals(false, ValidParentheses.isValid(input));
33+
}
34+
35+
static Stream<Arguments> provideInvalidTestCases() {
36+
return Stream.of(
37+
Arguments.of("("),
38+
Arguments.of(")"),
39+
Arguments.of("([)]"),
40+
Arguments.of("{[}]"),
41+
Arguments.of("((()")
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)