Generate Parentheses

Subsequences Pattern DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Generate Parenthesis Problem, Recursion, Backtracking, Strings, Combinatorics, Time Complexity, Space Complexity, backtracking, dfs, stack, combinatorics, recursion, Graph Traversal (DFS).

Problem: Generate Parentheses Problem Statement Given an integer n, generate all combinations of well formed parentheses. A string of parentheses is "well formed" if: 1. Every open parenthesis has a corresponding closed parenthesis. 2. Every closed parenthesis has a corresponding open parenthesis. 3. Parentheses are closed in the correct order (e.g., "()" is well formed, ")(", "(()" are not). Input A single integer n (1 <= n <= 8). Output A list of strings, where each string represents a a valid combination of n pairs of parentheses. The order of strings in the list does not matter. Examples Example 1: Input: n = 1 Output: ["()"] Example 2: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] (Order of strings may vary)