We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 50e3c9d commit de37275Copy full SHA for de37275
src/main/java/com/thealgorithms/stacks/ValidParentheses.java
@@ -19,10 +19,15 @@ public static boolean isValid(final String s) {
19
20
Deque<Character> stack = new ArrayDeque<>();
21
for (char ch : s.toCharArray()) {
22
- if (PAIRS.containsValue(ch)) {
+ if (PAIRS.containsValue(ch)) { // opening bracket
23
stack.push(ch);
24
- } else if (PAIRS.containsKey(ch)) {
25
- if (stack.isEmpty() || stack.pop() != PAIRS.get(ch)) {
+ } else if (PAIRS.containsKey(ch)) { // closing bracket
+ // Split logic to satisfy PMD
26
+ if (stack.isEmpty()) {
27
+ return false;
28
+ }
29
+ Character top = stack.pop();
30
+ if (top != PAIRS.get(ch)) {
31
return false;
32
}
33
0 commit comments