Backtracking pattern

Generate Parentheses

Given an integer n, return all distinct strings of n pairs of parentheses that are well-formed (every opening parenthesis is properly matched and closed). Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 22
  • Medium
  • 1 approach

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

BacktrackingLeetCode 22

Medium

The approach

  1. 01 Backtracking · open/close counters with pruning O(4ⁿ / √n)O(n) space
Played step by step, with its time and space.

What you will learn

  • How to read Generate Parentheses and spot the backtracking pattern in it
  • Backtracking · open/close counters with pruning: O(4ⁿ / √n) time, O(n) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Backtracking

See Generate Parentheses run, step by step

Sign in and this page opens on the animation, with all 8 Backtracking problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.