
- 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
C++ program to find n valid bracket sequences
Suppose we have a number n. As we know, a bracket sequence is a string containing only characters "(" and ")". A valid bracket sequence is a bracket sequence which can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. So, if a bracket sequence is like "()()" this is valid because we can put 1's like "(1)+(1)". From number n, we have to find exactly n different possible valid bracket sequences of length 2n.
So, if the input is like n = 4, then the output will be ["()()()()", "(())()()", "((()))()", "(((())))"]
Steps
To solve this, we will follow these steps −
for initialize k := 1, when k <= n, update (increase k by 1), do: for initialize i := 1, when i <= k, update (increase i by 1), do: print "(" for initialize i := 1, when i <= k, update (increase i by 1), do: print ")" for initialize i := k + 1, when i <= n, update (increase i by 1), do: print "()" go to next line
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= k; i++) cout << "("; for (int i = 1; i <= k; i++) cout << ")"; for (int i = k + 1; i <= n; i++) cout << "()"; cout << endl; } } int main() { int n = 4; solve(n); }
Input
4
Output
()()()() (())()() ((()))() (((())))
- Related Articles
- C++ Program to find number of RBS string we can form bracket sequences
- C++ Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
- C# Program to merge sequences
- Program to find minimum remove required to make valid parentheses in Python
- Find index of closing bracket for a given opening bracket in an expression in C++
- Program to find latest valid time by replacing hidden digits in Python
- Program to find valid matrix given row and column sums in Python
- Program to check whether a board is valid N queens solution or not in python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of strictly increasing colorful candle sequences are there in Python
- C++ Program to find pairs of sequences where sequence holds minimum and maximum elements
- Program to find length of longest valid parenthesis from given string in Python
- Program to find the maximum score from all possible valid paths in Python
- Program to count number of characters in each bracket depth in Python
- C# Program to return the difference between two sequences

Advertisements