Tag Archives: well formed

C# || Longest Valid Parentheses – How To Find The Longest Valid Well Formed Parentheses Using C#

The following is a module with functions which demonstrates how to find the longest valid well formed parentheses using C#.


1. Longest Valid Parentheses – Problem Statement

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.

Example 1:


Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".

Example 2:


Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".

Example 3:


Input: s = ""
Output: 0


2. Longest Valid Parentheses – Solution

The following is a solution which demonstrates how to find the longest valid well formed parentheses.

In this solution we can make use of a stack while scanning the given string to:

  • Check if the string scanned so far is valid
  • Find the length of the longest valid string

In order to do so, we start by pushing -1 onto the stack. For every ‘(‘ encountered, we push its index onto the stack.

For every ‘)‘ encountered, we pop the topmost element. Then, the length of the currently encountered valid string of parentheses will be the difference between the current element’s index and the top element of the stack.

If, while popping the element, the stack becomes empty, we will push the current element’s index onto the stack. In this way, we can continue to calculate the length of the valid substrings and return the length of the longest valid string at the end.

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 for the example cases:


2
4
0

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.

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: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()

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.

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.

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: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()