C++ Program to find number of RBS string we can form bracket sequences

C++Server Side ProgrammingProgramming

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
raja
Updated on 08-Apr-2022 11:27:07

Advertisements