Print the string obtained after removal of outermost parentheses
Given a valid parenthesis string str consisting of lowercase alphabets, opening, and closing brackets, the task is to find the string by removing the outermost enclosing brackets such that the string remains a valid parenthesis string.
Examples:
Input: S = “(((a)(bcd)(e)))”
Output: (a)(bcd)(e)
Explanation:
The outermost enclosing brackets are: { S[0], S[1], S[13], S[14] }.
Removing the outermost enclosing brackets from str modifies str to “(a)(bcd)(e)”.
Therefore, the required output is (a)(bcd)(e).
Input: str = “((ab)(bc))d”
Output: ((ab)(bc))d
Explanation:
Since no outermost enclosing brackets present in the string. Therefore, the required output is ((ab)(bc))d
Approach:The idea is to iterate over the characters of the string and count the number of consecutive opening parenthesis and closing parenthesis from both ends of the string respectively. Then, iterate over the characters present in the inner string and count the number of opening brackets needed to balance out the string. Follow the steps below to solve the problem:
Follow the below steps to solve the problem:
- Initialize a variable, say cnt = 0, to store the count of valid parenthesis such that str[cnt] == ‘(‘ and str[N – cnt – 1] == ‘)’.
- To balance the inner parenthesis of the string by the outer parenthesis, traverse the substring {str[cnt], …, str[N – cnt – 1]} and count the minimum opening or closing parenthesis required to balance the inner substring say, cntMinpar.
- Finally, update the cnt += cntMinPair and print the substring { str[cnt], …, str[N – cnt] }.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeBrakets(string str)
{
int n = str.length();
int cnt = 0;
while (cnt < n && str[cnt] == '(' &&
str[n - cnt - 1] == ')' )
{
cnt++;
}
int error = 0;
int open = 0;
for ( int i = cnt; i < n - cnt; i++)
{
if (str[i] == '(' )
{
open++;
}
else if (str[i] == ')' )
{
if (open>0){
open--;
}
else {
error++;
}
}
}
int rem=cnt-error;
cout << str.substr(rem, n - 2*rem) << endl;
}
int main()
{
string str = "((((a)b)(c)))" ;
removeBrakets(str);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static void removeBrakets(String str)
{
int n = str.length();
int cnt = 0 ;
while (cnt < n && str.charAt(cnt) == '('
&& str.charAt(n - cnt - 1 ) == ')' ) {
cnt++;
}
int cntMinPar = 0 ;
int cntUnbal = 0 ;
for ( int i = cnt; i < n - cnt;
i++) {
if (str.charAt(i) == '(' ) {
cntUnbal++;
}
else if (str.charAt(i) == ')' ) {
cntUnbal--;
}
cntMinPar = Math.min(
cntMinPar, cntUnbal);
}
cnt += cntMinPar;
System.out.println(
str.substring(cnt, n - cnt));
}
public static void main(
String[] args)
{
String str = "((((a)b)(c)))" ;
removeBrakets(str);
}
}
|
Python3
def removeBrakets( str ):
n = len ( str )
cnt = 0
while (cnt < n and str [cnt] = = '(' and
str [n - cnt - 1 ] = = ')' ):
cnt + = 1
cntMinPar = 0
cntUnbal = 0
for i in range (cnt, n - cnt):
if ( str [i] = = '(' ):
cntUnbal + = 1
elif str [i] = = ')' :
cntUnbal - = 1
cntMinPar = min (cntMinPar,
cntUnbal)
cnt + = cntMinPar
print ( str [cnt: n - cnt])
if __name__ = = '__main__' :
str = "((((a)b)(c)))"
removeBrakets( str )
|
C#
using System;
class GFG {
static void removeBrakets(String str)
{
int n = str.Length;
int cnt = 0;
while (cnt < n && str[cnt] == '('
&& str[n - cnt - 1] == ')' )
{
cnt++;
}
int cntMinPar = 0;
int cntUnbal = 0;
for ( int i = cnt; i < n - cnt;
i++)
{
if (str[i] == '(' )
{
cntUnbal++;
}
else if (str[i] == ')' )
{
cntUnbal--;
}
cntMinPar = Math.Min(
cntMinPar, cntUnbal);
}
cnt += cntMinPar;
Console.WriteLine(
str.Substring(cnt, n - cnt - 2));
}
public static void Main( string [] args)
{
string str = "((((a)b)(c)))" ;
removeBrakets(str);
}
}
|
Javascript
<script>
function removeBrakets(str)
{
var n = str.length;
var cnt = 0;
while (cnt < n && str[cnt] == '(' &&
str[n - cnt - 1] == ')' )
{
cnt++;
}
var error = 0;
var open = 0;
for ( var i = cnt; i < n - cnt; i++)
{
if (str[i] == '(' )
{
open++;
}
else if (str[i] == ')' )
{
if (open > 0)
{
open--;
}
else
{
error++;
}
}
}
var rem = cnt - error;
document.write(str.substring(
rem, rem + n - 2 * rem));
}
var str = "((((a)b)(c)))" ;
removeBrakets(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
07 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...