Balanced Parentheses (Valid Parentheses)
Check if a string containing parentheses, brackets, and braces is properly balanced using a stack. A fundamental problem in parsing, compilers, and expression evaluation. Each opening bracket must have a matching closing bracket in correct order.
Visualization
Interactive visualization for Balanced Parentheses (Valid Parentheses)
Valid brackets: ( ) [ ]
Interactive visualization with step-by-step execution
Implementation
1function isValid(s: string): boolean {
2 const stack: string[] = [];
3 const pairs: Record<string, string> = {
4 ')': '(',
5 '}': '{',
6 ']': '['
7 };
8
9 for (const char of s) {
10 if (char === '(' || char === '{' || char === '[') {
11 stack.push(char);
12 } else {
13 if (stack.length === 0 || stack.pop() !== pairs[char]) {
14 return false;
15 }
16 }
17 }
18
19 return stack.length === 0;
20}Deep Dive
Theoretical Foundation
The Balanced Parentheses problem uses a stack to track unmatched opening brackets. The key insight is that closing brackets must match the most recent unmatched opening bracket (LIFO - Last In First Out). When we encounter an opening bracket, we push it onto the stack. When we encounter a closing bracket, we pop from the stack and check if it matches. If the stack is empty when we try to pop, there's no matching opening bracket. If at the end the stack isn't empty, some opening brackets weren't closed. Time: O(n) single pass, Space: O(n) worst case when all opening brackets.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Compiler syntax checking
Code editors (bracket matching)
Expression parsing
HTML/XML validation
Math expression validation
JSON/YAML parsing
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.