Valid Parentheses — Animation

Step through the stack-based solution. Edit the input, pick a variant, and watch each character processed.

Speed
Step 1 / 14…running

Input

([{}])

Stack (top on right)

empty

Action

Initialize empty stack.

Code

0function isValid(s) {
1 const map = { ')': '(', ']': '[', '}': '{' };
2 const stack = [];
3 for (const char of s) {
4 if (char in map) {
5 if (stack.pop() !== map[char]) return false;
6 } else {
7 stack.push(char);
8 }
9 }
10 return stack.length === 0;
11}