Skip to content

Commit de37275

Browse files
committed
fix: refactor if statements to satisfy PMD CollapsibleIfStatements
1 parent 50e3c9d commit de37275

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ public static boolean isValid(final String s) {
1919

2020
Deque<Character> stack = new ArrayDeque<>();
2121
for (char ch : s.toCharArray()) {
22-
if (PAIRS.containsValue(ch)) {
22+
if (PAIRS.containsValue(ch)) { // opening bracket
2323
stack.push(ch);
24-
} else if (PAIRS.containsKey(ch)) {
25-
if (stack.isEmpty() || stack.pop() != PAIRS.get(ch)) {
24+
} else if (PAIRS.containsKey(ch)) { // closing bracket
25+
// Split logic to satisfy PMD
26+
if (stack.isEmpty()) {
27+
return false;
28+
}
29+
Character top = stack.pop();
30+
if (top != PAIRS.get(ch)) {
2631
return false;
2732
}
2833
}

0 commit comments

Comments
 (0)