## C# || How To Find All Combinations Of Well-Formed Brackets Using C#

The following is a program with functions which demonstrates how to find all combinations of well-formed brackets using C#.

The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1…n. For example, Brackets(3), the output would be:

`()`

(()) ()()

((())) (()()) (())() ()(()) ()()()

The number of possible combinations is the Catalan number of N pairs C(n).

1. Find All Well-Formed Brackets

The example below demonstrates the use of the ‘**Brackets**‘ function to find all the well-formed bracket combinations.

```
```
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
// ============================================================================// Author: Kenneth Perkins// Date: Sep 1, 2021// Taken From: http://programmingnotes.org/// File: Program.cs// Description: Generate all combinations of well-formed brackets from 1…n// ============================================================================using System;using System.Collections.Generic; public class Program { static void Main(string[] args) { var results = Brackets(4); foreach (var result in results) { Console.WriteLine("Pair: " + result.pair + ", Combination: " + result.combination); } Console.ReadLine(); } /// <summary> /// Returns all combinations of well-formed brackets from 1...n /// </summary> /// <param name="pairs">The number of bracket combinations to generate</param> /// <param name="open">Optional. The 'open bracket' symbol</param> /// <param name="close">Optional. The 'close bracket' symbol</param> /// <returns>A List of bracket combination info</returns> public static List<Result> Brackets(int pairs, string open = "(", string close = ")") { var results = new List<Result>(); var symbols = new Symbols { open = open, close = close }; for (int pair = 1; pair <= pairs; ++pair) { var result = new Result { pair = pair, combination = BuildBrackets("", 0, 0, pair, symbols) }; results.Add(result); } return results; } public static string BuildBrackets(string output, int open, int close, int pair, Symbols symbols) { if ((open == pair) && (close == pair)) { return output; } string result = ""; if (open < pair) { string openCombo = BuildBrackets(output + symbols.open, open + 1, close, pair, symbols); if (openCombo.Length > 0) { result += (result.Length > 0 ? ", " : "") + openCombo; } } if (close < open) { string closeCombo = BuildBrackets(output + symbols.close, open, close + 1, pair, symbols); if (closeCombo.Length > 0) { result += (result.Length > 0 ? ", " : "") + closeCombo; } } return result; } public class Result { public int pair { get; set; } public string combination { get; set; } } public class Symbols { public string open { get; set; } public string close { get; set; } }}// http://programmingnotes.org/

**QUICK NOTES**:

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Pair: 1, Combination: ()

Pair: 2, Combination: (()), ()()

Pair: 3, Combination: ((())), (()()), (())(), ()(()), ()()()

Pair: 4, Combination: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()

## Leave a Reply