
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all combinations of balanced parentheses in C++
In this problem, we are given an integer n. Our task is to print all possible pairs of n balanced parentheses.
Balanced parentheses are parentheses pairs that have a closing symbol for every corresponding opening symbol. Also, pairs should be properly nested.
Let’s take an example to understand the problem,
Input: n = 2 Output: {}{} {{}}
To solve this problem, we need to keep track of pairs of brackets. The initial count of brackets is 0. Then we will recursively a function till the total bracket count is less than n. Count brackets, recursively call for brackets based on the count. If opening bracket count is more than closing, put closing brackets and then go for a remaining count of pairs, if the opening bracket is less than n recursively call for remaining bracket pairs.
Example
The below code with show implementation of our solution,
# include<iostream> using namespace std; # define MAX_COUNT 100 void printParenthesesPairs(int pos, int n, int open, int close){ static char str[MAX_COUNT]; if(close == n) { cout<<str<<endl; return; } else { if(open > close) { str[pos] = '}'; printParenthesesPairs(pos+1, n, open, close+1); } if(open < n) { str[pos] = '{'; printParenthesesPairs(pos+1, n, open+1, close); } } } int main() { int n = 3; cout<<"All parentheses pairs of length "<<n<<" are:\n"; if(n > 0) printParenthesesPairs(0, n, 0, 0); getchar(); return 0; }
Output
All parentheses pairs of length 3 are − {}{}{} {}{{}} {{}}{} {{}{}} {{{}}}
- Related Articles
- Print all combinations of factors in C++
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Print all the combinations of a string in lexicographical order in C++
- Check for balanced parentheses in an expression in C++
- Print all combinations of points that can compose a given number in C++
- Check for balanced parentheses in Python
- Print all possible combinations of r elements in a given array of size n in C++
- Program to find maximum number of balanced groups of parentheses in Python
- Program to check whether parentheses are balanced or not in Python
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- Score of Parentheses in C++
- Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++
- All combinations of sums for array in JavaScript
- Generate all combinations of supplied words in JavaScript
- All pair combinations of 2 tuples in Python
