Similar Problems

Similar Problems not available

Generate Parantheses - Leetcode Solution

LeetCode:  Generate Parantheses Leetcode Solution

Difficulty: Medium

Topics: backtracking  

For a given n , generate all strings of size 2*n with well formed parantheses.

Example Test Cases

Sample Test Case 1

N: 2
Expected Output:

  • ()()
  • (())
Sample Test Case 2

N: 3
Expected Output:

  • ((()))
  • (()())
  • ()(())
  • (())()
  • ()()()

Solution

We can solve this problem by generating all the possible strings of length 2^n , such that each character in the string is either ( or ) , and then printing only those strings which are balanced. There will be a total of `2`2n such strings.

Implementation

We can implement the above algorithm using backtracking as shown below:

Generate Parantheses Solution Code

1