C++ Remove Invalid Parentheses from an Expression

C++Server Side ProgrammingProgramming

Given a parentheses sequence; now, you have to print all the possible parentheses that it can make by removing the invalid brackets, for example

Input : str = “()())()” -
Output : ()()() (())()
There are two possible solutions
"()()()" and "(())()"

Input : str = (v)())()
Output : (v)()() (v())()

In this problem, we are going to use backtracking so that it will print all the valid sequences.

Approach to Find the Solution

In this approach, we will be trying to remove the opening and closing brackets one by one using BFS. Now for each sequence, we check if it is valid or not. If it is valid, then we print it as our output.


#include <bits/stdc++.h>
using namespace std;
bool isParenthesis(char c){
    return ((c == '(') || (c == ')'));
bool validString(string str){
    // cout << str << " ";
    int cnt = 0;
    for (int i = 0; i < str.length(); i++){
        if (str[i] == '(')
        else if (str[i] == ')')
        if (cnt < 0)
           return false;
    // cout << str << " ";
    return (cnt == 0);
void validParenthesesSequences(string str){
    if (str.empty())
        return ;
    set<string> visit; // if we checked that sting so we put it inside visit
                      // so that we will not encounter that string again
    queue<string> q; // queue for performing bfs
    string temp;
    bool level;
    // pushing given string as starting node into queue
    while (!q.empty()){
        str = q.front(); q.pop();
        if (validString(str)){
        //    cout << "s";
            cout << str << "\n"; // we print our string
            level = true; // as we found the sting on the same level so we don't need to apply bfs from it
        if (level)
        for (int i = 0; i < str.length(); i++){
            if (!isParenthesis(str[i])) // we won't be removing any other characters than the brackets from our string
            temp = str.substr(0, i) + str.substr(i + 1); // removing parentheses from the strings one by one
            if (visit.find(temp) == visit.end()) { // if we check that string so we won't check it again
int main(){
    string s1;
    s1 = "(v)())()";
    cout << "Input : " << s1 << "\n";
    cout << "Output : ";
    return 0;


Input : (v)())()
Output : (v())()

Explanation of the Above Code

In the above approach, we simply one by one remove our parentheses now as we can the bracket we also keep track of the previous sequences so that we won’t check the same sequence twice now if we find a valid sequence out of these all possibilities, we print all the valid possibilities and that’s how our program proceeds.


In this tutorial, we solve a problem to find Remove Invalid Parentheses. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Published on 26-Nov-2021 06:23:33