- 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 number of RBS string we can form bracket sequences

Suppose we have a string S with brackets. There are two types of brackets '(' ')' and '[', ']'. A string is called a regular bracket sequence (RBS) if it follows following types −

- empty string;
- '(' | RBS | ')';
- '[' | RBS | ']';
- RBS | RBS.

Where '|' is a concatenation of two strings. In one move we can select a non-empty subsequence of the string S (not necessarily consecutive) that is an RBS, then remove it from the string and concatenate the remaining parts without changing the order. We have to find the maximum number of moves we can perform.

## Problem Category

The above-mentioned problem can be solved by applying Greedy problem-solving techniques. The greedy algorithm techniques are types of algorithms where the current best solution is chosen instead of going through all possible solutions. Greedy algorithm techniques are also used to solve optimization problems, like its bigger brother dynamic programming. In dynamic programming, it is necessary to go through all possible subproblems to find out the optimal solution, but there is a drawback of it; that it takes more time and space. So, in various scenarios greedy technique is used to find out an optimal solution to a problem. Though it does not gives an optimal solution in all cases, if designed carefully it can yield a solution faster than a dynamic programming problem. Greedy techniques provide a locally optimal solution to an optimization problem. Examples of this technique include Kruskal's and Prim's Minimal Spanning Tree (MST) algorithm, Huffman Tree coding, Dijkstra's Single Source Shortest Path problem, etc.

https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm

https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm

So, if the input of our problem is like S = "([)]", then the output will be 2, because if we remove '(' and ')', it will hold "[]" as substring and which is RBS, now remove them to get empty string which is also RBS.

## Steps

To solve this, we will follow these steps −

ans := 0 a := 0 b := 0 l := size of S for initialize i := 0, when i < l, update (increase i by 1), do: if S[i] is same as '(', then: (increase a by 1) if S[i] is same as '[', then: (increase b by 1) if S[i] is same as ')' and a > 0, then: (decrease a by 1) (increase ans by 1) if S[i] is same as ']' and b > 0, then: (decrease b by 1) (increase ans by 1) return ans

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(string S){ int ans = 0, a = 0, b = 0; int l = S.size(); for (int i = 0; i < l; i++){ if (S[i] == '(') a++; if (S[i] == '[') b++; if (S[i] == ')' && a > 0){ a--; ans++; } if (S[i] == ']' && b > 0){ b--; ans++; } } return ans; } int main(){ string S = "([)]"; cout << solve(S) << endl; }

## Input

"([)]"

## Output

2

- Related Articles
- C++ program to find n valid bracket sequences
- Program to find possible number of palindromes we can make by trimming string in Python
- Print all ways to break a string in bracket form in C++
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to count number of unique palindromes we can make using string characters in Python
- Program to find number of rectangles that can form the largest square in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find maximum number of people we can make happy in Python
- Program to count number of characters in each bracket depth in Python
- Program to find number of ways to form a target string given a dictionary in Python
- Program to find number of strictly increasing colorful candle sequences are there in Python
- Program to find number of ways we can arrange symbols to get target in Python?