Generate Parentheses
Given n pairs of parentheses Generate all combinations of well-formed parentheses Return them in any order Example: n = 3 => ["((()))","(()())","(())()","()(())","()()()"]
Given an integer n of at least 1, return every string made of n opening and n closing parentheses that is well formed, meaning each closing parenthesis matches an earlier unmatched opening one. Return the strings in any order, without duplicates. For example, n = 2 gives (()) and ()().
Examples
You write
generate_parenthesis(n: int)
Example 1
- Input
- n = 3
- returns
- ["((()))","(()())","(())()","()(())","()()()"]
Example 2
- Input
- n = 1
- returns
- ["()"]
The pattern behind it
Strings
I reach for it when
- The input is text and the answer is a count, a substring or a new string.
- You need to compare, reverse or rebuild characters.
Say this before you type. Say which array pattern the string is hiding before you touch a character.
All 51 Strings problemsBacktracking
I reach for it when
- The question asks for all combinations, permutations or placements.
- You make a choice, explore it, and may need to take it back.
Say this before you type. Say the choice, the stopping rule, and the earliest moment you can prune.
All 15 Backtracking problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.