Reduce string by removing outermost parentheses from each primitive substring
Given a string S of valid parentheses “(“ and “)”, the task is to print the string obtained by removing the outermost parentheses of every primitive substring from S.
A valid parentheses substring S is primitive if it is non-empty, and cannot be split into two or more non-empty substrings which are also a valid parentheses.
Examples:
Input: S = “(()())(())()”
Output: ()()()
Explanation: The input string is “(()())(())()” can be decomposed into primitive substrings “(()())” + “(())”+”()”. After removing outermost parentheses of each primitive substrings, the string obtained is “()()” + “()” = “()()()”
Input: S = “((()())(())(()(())))”
Output: (()())(())(()(()))
Approach: Follow the steps below to solve the problem:
- Initialize a variable count to store the number of opening parentheses, i.e. ‘(‘.
- Add every ‘(‘ to the result if count is greater than 0, i.e. add all ‘(‘ after the first ‘(‘ of a primitive substring is encountered.
- Add every ‘)’ to the result if count is greater than 1, i.e. add all ‘)’ before the last ‘)’ of a primitive substring is encountered.
- Finally, print the resultant string obtained.
Below is the implementation of the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
string removeOuterParentheses(string S)
{
string res;
int count = 0;
for ( char c : S) {
if (c == '(' && count++ > 0)
res += c;
if (c == ')' && count-- > 1)
res += c;
}
return res;
}
int main()
{
string S = "(()())(())()" ;
cout << removeOuterParentheses(S);
}
|
Java
import java.io.*;
class GFG{
static String removeOuterParentheses(String S)
{
String res = "" ;
int count = 0 ;
for ( int c = 0 ;
c < S.length(); c++)
{
if (S.charAt(c) == '(' &&
count++ > 0 )
res += S.charAt(c);
if (S.charAt(c) == ')' &&
count-- > 1 )
res += S.charAt(c);
}
return res;
}
public static void main(String[] args)
{
String S = "(()())(())()" ;
System.out.print(removeOuterParentheses(S));
}
}
|
Python3
def removeOuterParentheses(S):
res = ""
count = 0
for c in S:
if (c = = '(' and count > 0 ):
res + = c
if (c = = '(' ):
count + = 1
if (c = = ')' and count > 1 ):
res + = c
if (c = = ')' ):
count - = 1
return res
if __name__ = = '__main__' :
S = "(()())(())()"
print (removeOuterParentheses(S))
|
C#
using System;
class GFG{
static string removeOuterParentheses( string S)
{
string res = "" ;
int count = 0;
for ( int c = 0; c < S.Length; c++)
{
if (S == '(' &&
count++ > 0)
res += S;
if (S == ')' &&
count-- > 1)
res += S;
}
return res;
}
public static void Main()
{
string S = "(()())(())()" ;
Console.Write(removeOuterParentheses(S));
}
}
|
Javascript
<script>
function removeOuterParentheses(S)
{
let res = "" ;
let count = 0;
for (let c = 0;
c < S.length; c++)
{
if (S.charAt(c) == '(' &&
count++ > 0)
res += S.charAt(c);
if (S.charAt(c) == ')' &&
count-- > 1)
res += S.charAt(c);
}
return res;
}
let S = "(()())(())()" ;
document.write(removeOuterParentheses(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 string.
The Optimal approach to remove the outermost parentheses from a string can be achieved using a simple algorithm that keeps track of the number of opening and closing parentheses encountered. Here is an optimal approach:
- Initialize two variables, open_count and close_count, to zero
- Initialize an empty string called result.
- Loop through each character c in the input string s.
- If c is an opening parenthesis, increment open_count.
- If c is a closing parenthesis, increment close_count.
- If open_count and close_count are equal and greater than zero, this means that we have encountered a complete pair of opening and closing parentheses, so we can add the substring between them to the result string.
- Reset open_count and close_count to zero.
- Return the result string.
Here is an implementation of this algorithm:
C++
#include <iostream>
#include <string>
using namespace std;
string removeOuterParentheses(string s) {
int openCount = 0;
int closeCount = 0;
string result = "" ;
int start = 0;
for ( int i = 0; i < s.length(); i++) {
char c = s[i];
if (c == '(' ) {
openCount++;
} else if (c == ')' ) {
closeCount++;
}
if (openCount == closeCount) {
result += s.substr(start+1, i-start-1);
start = i+1;
}
}
return result;
}
int main() {
string s1 = "(()())(())()" ;
cout << removeOuterParentheses(s1) << endl;
string s2 = "()()(()())(()())" ;
cout << removeOuterParentheses(s2) << endl;
string s3 = "((()))(())" ;
cout << removeOuterParentheses(s3) << endl;
return 0;
}
|
Java
public class Main {
public static String removeOuterParentheses(String s) {
int openCount = 0 ;
int closeCount = 0 ;
String result = "" ;
int start = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' ) {
openCount++;
} else if (c == ')' ) {
closeCount++;
}
if (openCount == closeCount) {
result += s.substring(start+ 1 , i);
start = i+ 1 ;
}
}
return result;
}
public static void main(String[] args) {
String s1 = "(()())(())()" ;
System.out.println(removeOuterParentheses(s1));
String s2 = "()()(()())(()())" ;
System.out.println(removeOuterParentheses(s2));
String s3 = "((()))(())" ;
System.out.println(removeOuterParentheses(s3));
}
}
|
Python3
def remove_outer_parentheses(s):
open_count = 0
close_count = 0
result = ""
start = 0
for i, c in enumerate (s):
if c = = "(" :
open_count + = 1
elif c = = ")" :
close_count + = 1
if open_count = = close_count:
result + = s[start + 1 :i]
start = i + 1
return result
if __name__ = = "__main__" :
s1 = "(()())(())()"
print (remove_outer_parentheses(s1))
s2 = "()()(()())(()())"
print (remove_outer_parentheses(s2))
s3 = "((()))(())"
print (remove_outer_parentheses(s3))
|
C#
using System;
namespace RemoveOuterParentheses {
class Program {
static string RemoveOuterParentheses( string s)
{
int openCount = 0;
int closeCount = 0;
string result = "" ;
int start = 0;
for ( int i = 0; i < s.Length; i++) {
char c = s[i];
if (c == '(' ) {
openCount++;
}
else if (c == ')' ) {
closeCount++;
}
if (openCount == closeCount) {
result += s.Substring(start + 1,
i - start - 1);
start = i + 1;
}
}
return result;
}
static void Main( string [] args)
{
string s1 = "(()())(())()" ;
Console.WriteLine(RemoveOuterParentheses(s1));
string s2 = "()()(()())(()())" ;
Console.WriteLine(RemoveOuterParentheses(s2));
string s3 = "((()))(())" ;
Console.WriteLine(RemoveOuterParentheses(s3));
}
}
}
|
Javascript
function remove_outer_parentheses(s) {
let open_count = 0;
let close_count = 0;
let result = "" ;
let start = 0;
for (let i = 0; i < s.length; i++) {
let c = s[i];
if (c == "(" ) {
open_count += 1;
} else if (c == ")" ) {
close_count += 1;
}
if (open_count == close_count) {
result += s.slice(start + 1, i);
start = i + 1;
}
}
return result;
}
let s1 = "(()())(())()" ;
console.log(remove_outer_parentheses(s1));
let s2 = "()()(()())(()())" ;
console.log(remove_outer_parentheses(s2));
let s3 = "((()))(())" ;
console.log(remove_outer_parentheses(s3));
|
Output
()()()
()()()()
(())()
This approach has a time complexity of O(n), where n is the length of the input string s. It uses constant space, except for the output string, which is proportional to the number of valid parentheses pairs in s.
Last Updated :
03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...