diff --git a/DataStructures/Stacks/BalancedBrackets.java b/DataStructures/Stacks/BalancedBrackets.java index 1a8673d6..8063c510 100644 --- a/DataStructures/Stacks/BalancedBrackets.java +++ b/DataStructures/Stacks/BalancedBrackets.java @@ -38,29 +38,25 @@ class BalancedBrackets { bracketsStack.push(x); break; case '}': - if (bracketsStack.peek() == '{') { - bracketsStack.pop(); + if (!bracketsStack.empty() && bracketsStack.pop() == '{') { break; } else { return false; } case '>': - if (bracketsStack.peek() == '<') { - bracketsStack.pop(); + if (!bracketsStack.empty() && bracketsStack.pop() == '<') { break; } else { return false; } case ')': - if (bracketsStack.peek() == '(') { - bracketsStack.pop(); + if (!bracketsStack.empty() && bracketsStack.pop() == '(') { break; } else { return false; } case ']': - if (bracketsStack.peek() == '[') { - bracketsStack.pop(); + if (!bracketsStack.empty() && bracketsStack.pop() == '[') { break; } else { return false;