DSA Visualizer · 25 visualizationsValid Parentheses
Arrays & Hashing
Two SumValid AnagramContains DuplicateGroup AnagramsTop K Frequent ElementsProduct of Array Except SelfLongest Consecutive SequenceBinary Search
Binary SearchTrees & BFS
Binary Tree Level OrderGraphs
Number of IslandsTrees
Invert Binary TreeEasyStack
Valid Parentheses
Given a string s containing just the characters (, ), {, }, [ and ], determine if the input string is valid. An input string is valid if: - Open brackets must be closed by the same type of brackets. - Open brackets must be closed in the correct order. - Every close bracket has a corresponding open bracket.
The step-through player is coming for this one. Two Sum has the full animation today; the rest are being authored in the admin.
Pythontime O(n) · space O(n)
def isValid(s: str) -> bool:
stack = []
pairs = {')': '(', '}': '{', ']': '['}
for ch in s:
if ch in '([{':
stack.append(ch)
elif not stack or stack[-1] != pairs[ch]:
return False
else:
stack.pop()
return len(stack) == 0Next upBinary Search
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough