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,
- Leetcode Generate Parentheses problem solution in C++
- Leetcode Generate Parentheses problem solution in Java
- Leetcode Generate Parentheses problem solution in Python
- Leetcode Generate Parentheses problem solution in C