Remove Invalid Parentheses in C++



Suppose we have a string of parentheses. We have to remove minimum number of invalid parentheses and return the well formed parentheses strings, So if the input is like “()(()()”, so the result will be [“()()()”, “()(())”]

To solve this, we will follow these steps −

  • Define a method called solve(), this will take pos, left, right, l, r, string array res and string temp.

  • if pos is same as size of s, then,

    • if left is same as 0 and right is same as 0, then,

      • if (m[temp] is non-zero) is false, then,

        • insert temp at the end of res

      • m[temp] := 1

    • return

  • if s[pos] is same as '(' and right > 0, then,

    • call solve(pos + 1, left, right - 1, l, r, res, temp + empty string)

  • Otherwise when s[pos] is same as ')' and left > 0, then −

    • call solve(pos + 1, left - 1, right, l, r, res, temp + empty string)

  • if s[pos] is same as '(', then,

    • call solve(pos + 1, left, right, l + 1, r, res, temp + "(")

  • Otherwise when s[pos] is same as ')' and l > r, then −

    • call solve(pos + 1, left, right, l, r + 1, res, temp + ')')

  • if s[pos] is not equal to '(' and s[pos] is not equal to ')', then,

    • call solve(pos + 1, left, right, l, r, res, temp + s[pos])

  • From the main method do the following −

  • Make one array res

  • l := 0, r := 0

  • for initializing i := 0, when i< size of s, increase i by 1 do −

    • if s[i] is same as '(', then,

      • increase r by 1

    • Otherwise when s[i] is same as ')', then −

      • if r is non-zero, then, decrease r by 1

      • increase else l by 1

  • Call the function solve(0,l,r,0,0,res)

  • return res

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   string s;
   map <string ,int> m;
   void solve(int pos, int left, int right,int l, int r, vector <string> &res, string temp=""){
      if(pos == s.size()){
         if(left==0 && right==0 && l==r){
            if(!m[temp])
               res.push_back(temp);
            m[temp] = 1;
         }
         return;
      }
      if(s[pos] =='(' && right>0 ){
         solve(pos+1,left,right-1,l,r,res,temp+"");
      } else if(s[pos] ==')' && left>0) {
         solve(pos+1,left-1,right,l,r,res,temp+"");
      }
      if(s[pos] =='(')solve(pos+1,left,right,l+1,r,res,temp+"(");
      else if(s[pos] == ')' && l>r)solve(pos+1,left,right,l,r+1,res,temp+')');
      if(s[pos]!='(' && s[pos]!=')')solve(pos+1,left,right,l,r,res,temp+s[pos]);
   }
   vector<string> removeInvalidParentheses(string s) {
      vector <string > res;
      int l = 0;
      int r=0;
      this->s = s;
      for(int i =0;i<s.size();i++){
         if(s[i] == '('){
            r++;
         } else if(s[i]==')') {
            if(r)r--;
            else l++;
         }
      }
      solve(0,l,r,0,0,res);
      return res;
   }
};
main(){
   Solution ob;
   print_vector(ob.removeInvalidParentheses("()(()()"));
}

Input

“()(()()”

Output

[()()(), ()(()), ]
raja
Published on 27-May-2020 05:52:37
Advertisements