Number of closing brackets needed to complete a regular bracket sequence
Last Updated :
09 Sep, 2022
Given an incomplete bracket sequence S. The task is to find the number of closing brackets ‘)’ needed to make it a regular bracket sequence and print the complete bracket sequence. You are allowed to add the brackets only at the end of the given bracket sequence. If it is not possible to complete the bracket sequence, print “IMPOSSIBLE”.
Let us define a regular bracket sequence in the following way:
- Empty string is a regular bracket sequence.
- If s is a regular bracket sequence, then (s) is a regular bracket sequence.
- If s & t are regular bracket sequences, then st is a regular bracket sequence.
Examples:
Input : str = “(()(()(”
Output : (()(()()))
Explanation : The minimum number of ) needed to make the sequence regular are 3 which are appended at the end.
Input : str = “())(()”
Output : IMPOSSIBLE
Approach :
We need to add minimal number of closing brackets ‘)’, so we will count the number of unbalanced opening brackets and then we will add that amount of closing brackets. If at any point the number of the closing bracket is greater than the opening bracket then the answer is IMPOSSIBLE.
Algorithm :
- Create two variables open = 0 and close = 0
- Traverse on a string from i = 0 to i = n(size of string)
- If current element is ‘(‘ then increment open else if current element is ‘)’ then increment close.
- While traversing check if count of close is greater than open or not if yes then print Impossible return to main
- After completion of traversal calculate (open-close) as that many times closing brackets required to make the sequence balanced.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void completeSequence(string s)
{
int n = s.length();
int open = 0, close = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '(' )
open++;
else
close++;
if (close > open)
{
cout << "Impossible" << endl;
return ;
}
}
cout << s;
for ( int i = 0; i < open - close; i++)
cout << ')' ;
cout << endl;
}
int main()
{
string s = "(()(()(" ;
completeSequence(s);
return 0;
}
|
Java
class GFG {
static void completeSequence(String s)
{
int n = s.length();
int open = 0 , close = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '(' )
open++;
else
close++;
if (close > open) {
System.out.print( "IMPOSSIBLE" );
return ;
}
}
System.out.print(s);
for ( int i = 0 ; i < open - close; i++)
System.out.print( ")" );
}
public static void main(String[] args)
{
String s = "(()(()(" ;
completeSequence(s);
}
}
|
Python 3
def completeSequence(s):
n = len (s)
open = 0
close = 0
for i in range (n):
if (s[i] = = '(' ):
open + = 1
else :
close + = 1
if (close > open ):
print ( "IMPOSSIBLE" )
return
print (s, end = "")
for i in range ( open - close):
print ( ")" , end = "")
if __name__ = = "__main__" :
s = "(()(()("
completeSequence(s)
|
C#
using System;
class GFG
{
static void completeSequence(String s)
{
int n = s.Length;
int open = 0, close = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '(' )
open++;
else
close++;
if (close > open)
{
Console.Write( "IMPOSSIBLE" );
return ;
}
}
Console.Write(s);
for ( int i = 0; i < open - close; i++)
Console.Write( ")" );
}
static void Main()
{
String s = "(()(()(" ;
completeSequence(s);
}
}
|
PHP
<?php
function completeSequence( $s )
{
$n = strlen ( $s );
$open = 0;
$close = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] == '(' )
$open ++;
else
$close ++;
if ( $close > $open )
{
echo ( "IMPOSSIBLE" );
return ;
}
}
echo ( $s );
for ( $i = 0; $i < $open - $close ; $i ++)
echo ( ")" );
}
$s = "(()(()(" ;
completeSequence( $s );
?>
|
Javascript
<script>
function completeSequence(s)
{
let n = s.length;
let open = 0, close = 0;
for (let i = 0; i < n; i++)
{
if (s[i] == '(' )
open++;
else
close++;
if (close > open)
{
document.write( "IMPOSSIBLE" );
return ;
}
}
document.write(s);
for (let i = 0; i < open - close; i++)
document.write( ")" );
}
let s = "(()(()(" ;
completeSequence(s);
</script>
|
Complexity Analysis:
- Time Complexity: O(n) , where n is size of given string
- Auxiliary Space: O(1) , as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...