Leetcode Generate Parentheses problem solution in C# programming

In the Leetcode Generate Parentheses problem solution in C# programming Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Leetcode Generate Parentheses problem solution in C# programming

public class Solution
{
    public IList<string> GenerateParenthesis(int n)
    {
        var combos = new List<string>();
        GenerateParenthesis(n - 1, n, "(", combos);
        return combos;
    }
    
    public void GenerateParenthesis(int n, int c, string current, IList<string> combos)
    {
        if (n < 0 || c < n)
        {
            return;
        }
        else if (c == 0)
        {
            combos.Add(current);
            return;
        }
        
        GenerateParenthesis(n - 1, c, current + '(', combos);
        GenerateParenthesis(n, c - 1, current + ')', combos);
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *