Split Parenthesis Sequence into maximum valid Substrings
Last Updated :
09 Jan, 2023
Given a String S which contains balanced Parentheses Brackets ( i.e ‘(‘ and ‘)’ ), we need to split them in such a way that they form the maximum number of balanced groups.
Examples:
Input: S = “()()(()())()”
Output: (), (), (()()), ()
Input: S = “()()”
Output: (), ()
Input: S = “()()(())”
Output: (), (), (())
Approach: To solve the problem follow the below idea:
We need to declare two variables, one to keep the count of Opening Braces (say count_o) and the other to keep the count of the Closing Braces (i.e count _c). Then, we need to check when count_o and tcount _c is equal and then we need to push the current substring as a part of the result.
Follow the steps to solve the problem:
- Declare a vector (say res) to store the answer.
- Declare two counters count_o for opening braces and count_c for closing braces.
- Maintain a current substring by curr_str.
- Iterate through the string:
- Increment either count_o or count_c according to the combination of braces in the string.
- When count_o becomes equal to count_c, store the curr_str in res and redefine curr_str = “”.
- At last, return res as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector<string> Balanced_Groups(string s)
{
vector<string> res;
int count_o = 0;
int count_c = 0;
string curr_str = "" ;
int n = s.size();
if (n == 0)
return res;
for ( int i = 0; i < n; i++) {
curr_str += s[i];
if (s[i] == '(' ) {
count_o++;
}
if (s[i] == ')' ) {
count_c++;
}
if (count_o == count_c) {
res.push_back(curr_str);
curr_str = "" ;
}
}
return res;
}
int main()
{
string s = "()()(()())()" ;
vector<string> v;
v = Balanced_Groups(s);
for ( auto i : v)
cout << i << " " ;
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String s = "()()(()())()" ;
List<String> v = new ArrayList<>();
v = Balanced_Groups(s);
for (String i : v) {
System.out.print(i + " " );
}
}
public static List<String> Balanced_Groups(String s)
{
List<String> res = new ArrayList<>();
int count_o = 0 ;
int count_c = 0 ;
String curr_str = "" ;
int n = s.length();
if (n == 0 )
return res;
for ( int i = 0 ; i < n; i++) {
curr_str += s.charAt(i);
if (s.charAt(i) == '(' ) {
count_o++;
}
if (s.charAt(i) == ')' ) {
count_c++;
}
if (count_o == count_c) {
res.add(curr_str);
curr_str = "" ;
}
}
return res;
}
}
|
Python3
def Balanced_Groups(s):
res = []
count_o = 0
count_c = 0
curr_str = ""
n = len (s)
if n = = 0 :
return res
for i in range (n):
curr_str + = s[i]
if s[i] = = '(' :
count_o + = 1
if s[i] = = ')' :
count_c + = 1
if count_o = = count_c:
res.append(curr_str)
curr_str = ""
return res
if __name__ = = '__main__' :
s = "()()(()())()"
v = []
v = Balanced_Groups(s)
print (v)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main( string [] args)
{
string s = "()()(()())()" ;
List< string > v = new List< string >();
v = Balanced_Groups(s);
foreach ( string i in v) { Console.Write(i + " " ); }
}
public static List< string > Balanced_Groups( string s)
{
List< string > res = new List< string >();
int count_o = 0;
int count_c = 0;
string curr_str = "" ;
int n = s.Length;
if (n == 0)
return res;
for ( int i = 0; i < n; i++) {
curr_str += s[i];
if (s[i] == '(' ) {
count_o++;
}
if (s[i] == ')' ) {
count_c++;
}
if (count_o == count_c) {
res.Add(curr_str);
curr_str = "" ;
}
}
return res;
}
}
|
Javascript
function Balanced_Groups(s) {
let res = [];
let count_o = 0;
let count_c = 0;
let curr_str = "" ;
let n = s.length;
if (n == 0)
return res;
for (let i = 0; i < n; i++) {
curr_str += s[i];
if (s[i] == '(' ) {
count_o++;
}
if (s[i] == ')' ) {
count_c++;
}
if (count_o == count_c) {
res.push(curr_str);
curr_str = "" ;
}
}
return res;
}
let s = "()()(()())()" ;
let v;
v = Balanced_Groups(s);
for (let i of v)
process.stdout.write(i + " " );
|
Time Complexity: O(N), as only one for loop is used.
Auxiliary Space: O(N), an extra storage is used to store the valid substrings.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...