Count removal of pairs required to be empty all Balanced Parenthesis subsequences


C compiler treats a string as an array of characters, so it is easy to remove characters of a string based on their position. The first and last position of the string have to be checked for the presence of parenthesis and have to be removed. The string can be copied into another variable and presented.

There are many predefined functions in C that can be used effectively to manipulate strings. Removing a character from the starting or ending position is easily achieved in C with the help of the functions.

Removing starting and ending paranthesis from string

Paranthesis is a single character which is part of the input string and can be removed from the string by following the logic and algorithms as given below

A character is any alphanumeric key we see in the keyboard, it is stored in a character variable in C.

The () is called as parenthesis in c. We need to identify this character in the string input by the user and we need to remove it from the string.

An array is a variable which has many memory locations addressed by single name and consecutive numbering, and a string is an array of characters.

There are many situations in which we need to remove the parenthesis from a string, like in solving regular expressions.

Syntax

The function countRemoval takes in a string str as input and returns an integer value, which represents the number of removal of pairs required to make all the balanced parenthesis subsequences in the string empty. The function uses a variable count to keep track of the number of removals required, initially set to 0. It also uses a variable balance to keep track of the balance between the number of opening and closing parentheses in the string. The function then iterates through the length of the string and checks the character at each index. If the character is an opening parenthesis, the balance is incremented by 1, and if it's a closing parenthesis, the balance is decremented by 1. If the balance becomes negative, it means there is an extra closing parenthesis, and the count of removals is incremented by 1 and the balance is reset to 0. After the loop, the count is updated to include the remaining balance divided by 2 as the removals required to make all the balanced parenthesis subsequences in the string empty.

int countRemoval(string str) {
   int count = 0;
   int balance = 0;
   for (int i = 0; i < str.length(); i++) {
      if (str[i] == '(') {
         balance++;
      } else {
         balance--;
      }
      if (balance < 0) {
         count++;
         balance = 0;
      }
   }
   count += balance / 2;
   return count;
} 

Algorithm

  • Step 1 − Declare str1,str2, initialize to null.

  • Step 2 − Declare integer variables len,n,i

  • Step 3 − Accept str1 from console

  • Step 4 − Check if the first character is (

  • Step 5 − if yes n = 1

  • Step 6 − while n<length of input string len, copy each character from str1 into str2 </p>

  • Step 7 − check if last char of str2 is ).

  • Step 8 − If yes, replace it with \0.

  • Step 9 − Print str2 which has the input string minus ().

Approach

Approach 1 − Brute Force Recursion: In the first approach, we use brute force recursion to find all the possible subsequences and check if they are balanced or not. If a subsequence is balanced, we remove the pair of parentheses and count the number of pairs removed. We calculate the minimum number of pair removals required to empty the string.

Approach 2 − Dynamic Programming: The second approach uses dynamic programming to optimize the solution. We can use a 2D DP table to store the minimum number of removals required for a substring from index 'i' to 'j'. We iterate over the string and fill the DP table according to the given conditions.

Approach 1 Brute Force Recursion

Code

In this code, we check all possible combinations of subsequences, which leads to an exponential time complexity. This method may not be efficient for larger inputs.

#include <iostream>
#include <string>
using namespace std;

int countRemovalsRecursion(const string &s, int index, int open) {
   if (index == s.size()) {
      return open;
   }

   if (s[index] == '(') {
      return countRemovalsRecursion(s, index + 1, open + 1);
   } else if (open > 0) {
      return countRemovalsRecursion(s, index + 1, open - 1);
   } else {
      return 1 + countRemovalsRecursion(s, index + 1, open);
   }
}

int main() {
   string s = "(()())(";
   cout << "Input string: " << s << endl;
   cout << "Minimum removals (Brute Force Recursion): " << countRemovalsRecursion(s, 0, 0) << endl;
   return 0;
}

Output

Input string: (()())(
Minimum removals (Brute Force Recursion): 1

Approach 2

Example

This dynamic programming solution counts the minimum removals required to empty all balanced parenthesis subsequences. It iterates through the string, updating a 1D DP table with the number of open parentheses, and returns the final DP value as the minimum removals.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int countRemovalsDP(const string &s) {
   int n = s.size();
   vector<int> dp(n + 1, 0);

   for (int i = 0; i < n; ++i) {
      if (s[i] == '(') {
         dp[i + 1] = dp[i] + 1;
      } else {
         dp[i + 1] = max(dp[i] - 1, 0);
      }
   }
   return dp[n];
}

int main() {
   string s = "(()())()";
   cout << "Input string: " << s << endl;
   cout << "Minimum removals (Dynamic Programming): " << countRemovalsDP(s) << endl;
   return 0;
}

Output

Input string: (()())()
Minimum removals (Dynamic Programming): 0

Conclusion

Basic concepts of C, like what is the difference between the character and the string data type, the string delimiter, how to initialize the string and the array, the calculations that the first character of the array is the position with index 0 and that the last character has to be null is used in this program to get the correct output.

The removal of parenthesis is achieved in the program by implementing the basic, simple concepts of C programming.

Updated on: 20-Jul-2023

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements