Practice/Microsoft/Leetcode 22. Generate Parentheses
CodingOptional
Given an integer n representing the number of pairs of parentheses, generate all possible combinations of well-formed (balanced and correctly nested) parentheses strings.
A string of parentheses is considered well-formed if:
( has a corresponding closing parenthesis )Return all valid combinations in any order.
n opening and n closing parenthesesExample 1:
Input: n = 1 Output: ["()"] Explanation: Only one way to arrange a single pair of parentheses.
Example 2:
Input: n = 2 Output: ["(())", "()()"] Explanation: Two valid arrangements - nested or sequential.
Example 3:
Input: n = 3 Output: ["((()))", "(()())", "(())()", "()(())", "()()()"] Explanation: Five valid arrangements following all balancing rules.
Example 4:
Input: n = 0 Output: [""] Explanation: The empty string is the only valid sequence with zero pairs.