Java Program to Check Balanced Parentheses
stack.push() and popping when a matching closing bracket appears, returning true if all match and stack is empty at the end.Examples
How to Think About It
Algorithm
Code
import java.util.Stack; public class BalancedParentheses { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '{' || c == '[') { stack.push(c); } else if (c == ')' || c == '}' || c == ']') { if (stack.isEmpty()) return false; char top = stack.pop(); if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) { return false; } } } return stack.isEmpty(); } public static void main(String[] args) { String input = "()[]{}"; System.out.println(isBalanced(input) ? "Balanced" : "Not Balanced"); } }
Dry Run
Let's trace the input "()[]{}" through the code
Start with empty stack
stack = []
Read '(' - opening bracket
stack = ['(']
Read ')' - closing bracket matches '('
stack after pop = []
Read '[' - opening bracket
stack = ['[']
Read ']' - closing bracket matches '['
stack after pop = []
Read '{' - opening bracket
stack = ['{']
Read '}' - closing bracket matches '{'
stack after pop = []
End of string, stack empty
stack = [] means Balanced
| Character | Stack Content |
|---|---|
| ( | ['('] |
| ) | [] |
| [ | ['['] |
| ] | [] |
| { | ['{'] |
| } | [] |
Why This Works
Step 1: Use a stack to track opening brackets
A stack remembers the order of opening brackets so we can match them later with closing brackets.
Step 2: Match closing brackets with last opening bracket
When a closing bracket appears, it must match the last opening bracket on the stack to be balanced.
Step 3: Check stack emptiness at the end
If the stack is empty after processing all characters, all brackets matched correctly, so the string is balanced.
Alternative Approaches
public static boolean isBalancedSimple(String s) { int count = 0; for (char c : s.toCharArray()) { if (c == '(') count++; else if (c == ')') { if (count == 0) return false; count--; } } return count == 0; }
public static boolean isBalancedRecursive(String s) { if (s.isEmpty()) return true; int len = s.length(); for (int i = 0; i < len - 1; i++) { if ((s.charAt(i) == '(' && s.charAt(i+1) == ')') || (s.charAt(i) == '{' && s.charAt(i+1) == '}') || (s.charAt(i) == '[' && s.charAt(i+1) == ']')) { return isBalancedRecursive(s.substring(0, i) + s.substring(i+2)); } } return false; }
Complexity: O(n) time, O(n) space
Time Complexity
The program checks each character once, so it runs in linear time relative to the input length.
Space Complexity
The stack can hold up to all opening brackets, so in the worst case, space grows linearly with input size.
Which Approach is Fastest?
The stack approach is efficient and clear for all bracket types; simple counters are faster but limited to one bracket type.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Stack method | O(n) | O(n) | All bracket types, mixed input |
| Counter method | O(n) | O(1) | Only parentheses '()' |
| Recursive method | O(n^2) | O(n) | Small inputs, educational purposes |