Problem · String

Generate Parentheses

Learn this problem
MediumMicrosoft logoMicrosoftNEW GRADONSITE INTERVIEW
See Microsoft hiring insights

Problem statement

Given n pairs of parentheses, return every well-formed sequence that uses exactly those pairs.

Return the sequences in lexicographic order.

Function

generateParenthesis(n: int) → String[]

Examples

Example 1

n = 3return = ["((()))","(()())","(())()","()(())","()()()"]

These are all five balanced sequences containing three opening and three closing parentheses, in lexicographic order.

Constraints

  • 1 <= n <= 6

More Microsoft problems

drafts saved locally
public String[] generateParenthesis(int n) {
  // write your code here
}
n3
expected["((()))", "(()())", "(())()", "()(())", "()()()"]
checking account