Minimize length by removing subsequences forming valid parenthesis from a given string
Last Updated :
28 May, 2022
Given a string S consisting of ‘(‘, ‘)’, ‘[‘ and ‘]’, the task is to find the minimum count of remaining characters in the string by removing subsequences of the valid parenthesis.
Examples:
Input: S = “[]])([”
Output: 4
Explanation:
Removing the subsequence { str[0], str[1] } modifies S to “])([“.
Therefore, the required output is 4.
Input: S = “([)(])”
Output: 0
Explanation:
Removing the subsequence { str[0], str[2] } modifies S to “[(])”.
Removing the subsequence { str[0], str[2] } modifies S to “()”.
Removing the subsequence { str[0], str[1] } modifies S to “”.
Therefore, the required output is 0.
Approach: The problem can be solved using Stack. Follow the steps below to solve the problem:
- The idea is to handle the round parenthesis, ‘()’ and the bracket parenthesis, ‘[]’ in two separate stacks.
- Initialize two variables say, roundCount and squareCount to store the count of opening parenthesis in valid parenthesis of ‘()’ and ‘[]’ respectively.
- Iterate over each character of the given string and calculate the length of valid parenthesis of ‘()’ and ‘[]’ using two different stacks.
- Finally, print the value of (N – 2 * (roundCount + squareCount)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void deleteSubseq(string s)
{
int N = s.size();
stack< char > roundStk;
stack< char > squareStk;
int roundCount = 0;
int squareCount = 0;
for ( int i = 0; i < N; i++)
{
if (s[i] == '[' )
{
squareStk.push(s[i]);
}
else if (s[i] == ']' )
{
if (squareStk.size() != 0
&& squareStk.top() == '[' )
{
squareStk.pop();
squareCount += 1;
}
}
else if (s[i] == '(' )
{
roundStk.push(s[i]);
}
else
{
if (roundStk.size() != 0
&& squareStk.top() == '(' )
{
squareStk.pop();
roundCount += 1;
}
}
}
cout << (N - (2 * squareCount + 2 * roundCount));
}
int main()
{
string s = "[]])([" ;
deleteSubseq(s);
}
|
Java
import java.io.*;
import java.util.Stack;
class GFG
{
public static void deleteSubseq(String s)
{
int N = s.length();
Stack<Character> roundStk = new Stack<>();
Stack<Character> squareStk = new Stack<>();
int roundCount = 0 ;
int squareCount = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (s.charAt(i) == '[' )
{
squareStk.push(s.charAt(i));
}
else if (s.charAt(i) == ']' )
{
if (squareStk.size() != 0
&& squareStk.peek() == '[' )
{
squareStk.pop();
squareCount += 1 ;
}
}
else if (s.charAt(i) == '(' )
{
roundStk.push(s.charAt(i));
}
else
{
if (roundStk.size() != 0
&& squareStk.peek() == '(' )
{
squareStk.pop();
roundCount += 1 ;
}
}
}
System.out.println(
N - ( 2 * squareCount + 2 * roundCount));
}
public static void main(String[] args)
{
String s = "[]])([" ;
deleteSubseq(s);
}
}
|
Python3
def deleteSubseq(S):
N = len (S)
roundStk = []
squareStk = []
roundCount = 0
squareCount = 0
for i in S:
if i = = '[' :
squareStk.append(i)
elif i = = ']' :
if squareStk and squareStk[ - 1 ] = = '[' :
squareStk.pop()
squareCount + = 1
elif i = = '(' :
roundStk.append(i)
else :
if roundStk and roundStk[ - 1 ] = = '(' :
roundStk.pop()
roundCount + = 1
print (N - ( 2 * squareCount + 2 * roundCount))
if __name__ = = '__main__' :
S = '[]])(['
deleteSubseq(S)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void deleteSubseq(String s)
{
int N = s.Length;
Stack< char > roundStk = new Stack< char >();
Stack< char > squareStk = new Stack< char >();
int roundCount = 0;
int squareCount = 0;
for ( int i = 0; i < N; i++)
{
if (s[i] == '[' )
{
squareStk.Push(s[i]);
}
else if (s[i] == ']' )
{
if (squareStk.Count != 0
&& squareStk.Peek() == '[' )
{
squareStk.Pop();
squareCount += 1;
}
}
else if (s[i] == '(' )
{
roundStk.Push(s[i]);
}
else
{
if (roundStk.Count != 0
&& squareStk.Peek() == '(' )
{
squareStk.Pop();
roundCount += 1;
}
}
}
Console.WriteLine(
N - (2 * squareCount + 2 * roundCount));
}
public static void Main(String[] args)
{
String s = "[]])([" ;
deleteSubseq(s);
}
}
|
Javascript
<script>
function deleteSubseq(s)
{
var N = s.length;
var roundStk = [];
var squareStk = [];
var roundCount = 0;
var squareCount = 0;
for ( var i = 0; i < N; i++)
{
if (s[i] == '[' )
{
squareStk.push(s[i]);
}
else if (s[i] == ']' )
{
if (squareStk.length != 0
&& squareStk[squareStk.length-1] == '[' )
{
squareStk.pop();
squareCount += 1;
}
}
else if (s[i] == '(' )
{
roundStk.push(s[i]);
}
else
{
if (roundStk.length != 0
&& squareStk[squareStk.length-1] == '(' )
{
squareStk.pop();
roundCount += 1;
}
}
}
document.write(N - (2 * squareCount + 2 * roundCount));
}
var s = "[]])([" ;
deleteSubseq(s);
</script>
|
Time Complexity: O(N) where n is number of elements in given string. As, we are using a loop to traverse N times so it will cost us O(N) time.
Auxiliary Space: O(N), as we are using extra space for stack.
Share your thoughts in the comments
Please Login to comment...