Print all combinations of n balanced parentheses

Problem Implement an algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses. Example Input 3 (i.e., 3 pairs of parentheses) Output ()()(), ()(()), (())(), ((())) Solution This is a classic combinatorial problem that manifests itself in many different ways. Such kind of strings are called Dyck strings (see more here), which contain n X’s and n Y’s such that no initial segment of the string has more Y’s than X’s. [Read More]

Checking whether the paranthesis are balanced in the expression

Problem  Checking whether the paranthesis are balanced in the expression Solution Method 1 - Using stacks Stacks can be used to compare the paranthesis. Whenever we find the opening paranthesis we push it in the stack, and whenever we find closing paranthesis, we pop it out from stack. When the expression finishes and stack is not empty, then it means that paranthesis are not balanced otherwise they are. [Read More]

Stack ADT

Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure. [Read More]