Number of balanced parentheses substrings
Given a balanced parentheses string which consists of ‘(‘ and ‘)‘. The task is to find the number of balanced parentheses substrings in the given string
Examples :
Input : str = “()()()”
Output : 6
(), (), (), ()(), ()(), ()()()
Input : str = “(())()”
Output : 4
(), (()), (), (())()
Approach :
Let us assume that whenever we encounter with opening bracket the depth increases by one and with a closing bracket the depth decreases by one. Whenever we encounter the closing bracket increase our required answer by one and then increment our required answer by the already formed balanced substrings at this depth.
Below is the implementation of the above approach :
C++14
#include <bits/stdc++.h>
using namespace std;
int Balanced_Substring(string str, int n)
{
int ans = 0;
vector< int > arr(n / 2 + 1, 0);
int d = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '(' )
d++;
else {
if (d == 1) {
for ( int j = 2; j <= n / 2 + 1 && arr[j] != 0; j++)
arr[j] = 0;
}
++ans;
ans += arr[d];
arr[d]++;
d--;
}
}
return ans;
}
int main()
{
string str = "()()()" ;
int n = str.size();
cout << Balanced_Substring(str, n);
return 0;
}
|
Java
class GFG {
public static int Balanced_Substring(String str,
int n)
{
int ans = 0 ;
int [] arr = new int [n / 2 + 1 ];
int d = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '(' )
d++;
else {
if (d == 1 ) {
for ( int j = 2 ; j <= n / 2 + 1 && arr[j] != 0 ; j++)
arr[j] = 0 ;
}
++ans;
ans += arr[d];
arr[d]++;
d--;
}
}
return ans;
}
public static void main(String[] args)
{
String str = "()()()" ;
int n = str.length();
System.out.println(Balanced_Substring(str, n));
}
}
|
Python3
def Balanced_Substring(s, n):
ans = 0
arr = [ 0 ] * ( int (n / 2 ) + 1 )
d = 0
for i in range (n):
if (s[i] = = '(' ):
d + = 1
else :
if (d = = 1 ):
j = 2
while (j < = n / / 2 + 1 and arr[j] ! = 0 ):
arr[j] = 0
j + = 1
ans + = 1
ans + = arr[d]
arr[d] + = 1
d - = 1
return ans
s = "()()()"
n = len (s)
print (Balanced_Substring(s, n))
|
C#
using System;
class GFG {
public static int Balanced_Substring(String str,
int n)
{
int ans = 0;
int [] arr = new int [n / 2 + 1];
int d = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '(' )
d++;
else {
if (d == 1) {
for ( int j = 2; j <= n / 2 + 1 && arr[j] != 0; j++)
arr[j] = 0;
}
++ans;
ans += arr[d];
arr[d]++;
d--;
}
}
return ans;
}
public static void Main(String[] args)
{
String str = "()()()" ;
int n = str.Length;
Console.WriteLine(Balanced_Substring(str, n));
}
}
|
Javascript
<script>
function Balanced_Substring(str, n)
{
let ans = 0;
let arr = new Array(n / 2 + 1).fill(0);
let d = 0;
for (let i = 0; i < n; i++) {
if (str[i] == '(' )
d++;
else {
if (d == 1) {
for (let j = 2; j <= parseInt(n / 2) +
1 && arr[j] != 0; j++)
arr[j] = 0;
}
++ans;
ans += arr[d];
arr[d]++;
d--;
}
}
return ans;
}
let str = "()()()" ;
let n = str.length;
document.write(Balanced_Substring(str, n));
</script>
|
Time complexity : O(N)
Auxiliary Space: O(N)
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...