Generate N valid parentheses II

Hard

Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {}.

Assumptions

  • l, m, n >= 0

  • l + m + n > 0

Examples

l = 1, m = 1, n = 0, all the valid permutations are ["()<>", "(<>)", "<()>", "<>()"]

Solution: Recursive DFS 3 left branch one right branch

High Level:

Branch ( or < or { or complete

Base Case:

when no more braces to add && all braces are completed. Add to result

Recursive Rule:

  1. Add left brace until no more to add

  2. complete with right brace

  3. undo right brace

  4. undo left brace

Important note:

branching left first or right first does not matter. The key is to maintain a consistent state for each node by undoing previous add/remove push/pop.

Generic Solution:

Last updated

Was this helpful?