Skip to content

Commit 12286cf

Browse files
committed
fix: resolve Checkstyle, Clang format, and SpotBugs issues
1 parent c1e0a52 commit 12286cf

File tree

2 files changed

+29
-45
lines changed

2 files changed

+29
-45
lines changed

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,31 @@
33
import java.util.Stack;
44

55
/**
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>
6+
* Utility class to check for valid parentheses in a string.
237
*/
248
public final class ValidParentheses {
259

2610
private ValidParentheses() {
27-
// prevent instantiation
11+
throw new AssertionError("Cannot instantiate utility class");
2812
}
2913

3014
/**
31-
* Checks whether the input string has valid parentheses.
15+
* Returns true if the input string has valid parentheses.
3216
*
33-
* @param str input string containing parentheses
34-
* @return true if the string is valid, false otherwise
17+
* @params the string containing parentheses
18+
* @return true if valid, false otherwise
3519
*/
36-
public static boolean isValid(final String str) {
37-
Stack<Character> stack = new Stack<>();
20+
public static boolean isValid(final String s) {
21+
final Stack<Character> stack = new Stack<>();
3822

39-
for (char ch : str.toCharArray()) {
23+
for (final char ch : s.toCharArray()) {
4024
if (ch == '(' || ch == '{' || ch == '[') {
4125
stack.push(ch);
42-
} else {
26+
} else if (ch == ')' || ch == '}' || ch == ']') {
4327
if (stack.isEmpty()) {
4428
return false;
4529
}
46-
char top = stack.peek();
30+
final char top = stack.peek();
4731
if ((top == '(' && ch == ')')
4832
|| (top == '{' && ch == '}')
4933
|| (top == '[' && ch == ']')) {
@@ -56,13 +40,10 @@ public static boolean isValid(final String str) {
5640
return stack.isEmpty();
5741
}
5842

59-
/**
60-
* Example usage.
61-
*/
6243
public static void main(final String[] args) {
6344
System.out.println(isValid("()[]{}")); // true
64-
System.out.println(isValid("(]")); // false
65-
System.out.println(isValid("([)]")); // false
66-
System.out.println(isValid("{[]}")); // true
45+
System.out.println(isValid("(]")); // false
46+
System.out.println(isValid("([)]")); // false
47+
System.out.println(isValid("{[]}")); // true
6748
}
6849
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
11
package com.thealgorithms.stacks;
22

3-
43
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
55
import java.util.stream.Stream;
66
import org.junit.jupiter.params.ParameterizedTest;
77
import org.junit.jupiter.params.provider.Arguments;
88
import org.junit.jupiter.params.provider.MethodSource;
99

10+
/**
11+
* Tests for {@link ValidParentheses}.
12+
*/
1013
public class ValidParenthesesTest {
1114

1215
@ParameterizedTest
1316
@MethodSource("provideValidTestCases")
14-
void testIsValidValidCases(String input, boolean expected) {
17+
void testIsValidValidCases(String input, Boolean expected) {
1518
assertEquals(expected, ValidParentheses.isValid(input));
1619
}
1720

1821
static Stream<Arguments> provideValidTestCases() {
1922
return Stream.of(
20-
Arguments.of("()", true),
21-
Arguments.of("()[]{}", true),
22-
Arguments.of("{[]}", true),
23-
Arguments.of("", true) // empty string is valid
23+
Arguments.of("()", Boolean.TRUE),
24+
Arguments.of("()[]{}", Boolean.TRUE),
25+
Arguments.of("{[]}", Boolean.TRUE),
26+
Arguments.of("", Boolean.TRUE) // empty string is valid
2427
);
2528
}
2629

2730
@ParameterizedTest
2831
@MethodSource("provideInvalidTestCases")
29-
void testIsValidInvalidCases(String input) {
30-
assertEquals(false, ValidParentheses.isValid(input));
32+
void testIsValidInvalidCases(String input, Boolean expected) {
33+
assertEquals(expected, ValidParentheses.isValid(input));
3134
}
3235

3336
static Stream<Arguments> provideInvalidTestCases() {
3437
return Stream.of(
35-
Arguments.of("("),
36-
Arguments.of(")"),
37-
Arguments.of("([)]"),
38-
Arguments.of("{[}]"),
39-
Arguments.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)
4043
);
4144
}
4245
}

0 commit comments

Comments
 (0)