Print Bracket Number
Last Updated :
28 Sep, 2022
Given an expression exp of length n consisting of some brackets. The task is to print the bracket numbers when the expression is being parsed.
Examples :
Input : (a+(b*c))+(d/e)
Output : 1 2 2 1 3 3
The highlighted brackets in the given expression
(a+(b*c))+(d/e) has been assigned the numbers as:
1 2 2 1 3 3.
Input : ((())(()))
Output : 1 2 3 3 2 4 5 5 4 1
Source: Flipkart Interview Experience | Set 49.
Approach :
- Define a variable left_bnum = 1.
- Create a stack right_bnum.
- Now, for i = 0 to n-1.
- If exp[i] == ‘(‘, then print left_bnum, push left_bnum on to the stack right_bnum and finally increment left_bnum by 1.
- Else if exp[i] == ‘)’, then print the top element of the stack right_bnum and then pop the top element from the stack.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printBracketNumber(string exp , int n)
{
int left_bnum = 1;
stack< int > right_bnum;
for ( int i = 0; i < n; i++) {
if ( exp [i] == '(' ) {
cout << left_bnum << " " ;
right_bnum.push(left_bnum);
left_bnum++;
}
else if ( exp [i] == ')' ) {
cout << right_bnum.top() << " " ;
right_bnum.pop();
}
}
}
int main()
{
string exp = "(a+(b*c))+(d/e)" ;
int n = exp .size();
printBracketNumber( exp , n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void printBracketNumber(String exp,
int n)
{
int left_bnum = 1 ;
Stack<Integer> right_bnum =
new Stack<Integer>();
for ( int i = 0 ; i < n; i++)
{
if (exp.charAt(i) == '(' )
{
System.out.print(
left_bnum + " " );
right_bnum.push(left_bnum);
left_bnum++;
}
else if (exp.charAt(i) == ')' )
{
System.out.print(
right_bnum.peek() + " " );
right_bnum.pop();
}
}
}
public static void main(String args[])
{
String exp = "(a+(b*c))+(d/e)" ;
int n = exp.length();
printBracketNumber(exp, n);
}
}
|
Python3
def printBracketNumber(exp, n):
left_bnum = 1
right_bnum = list ()
for i in range (n):
if exp[i] = = '(' :
print (left_bnum, end = " " )
right_bnum.append(left_bnum)
left_bnum + = 1
elif exp[i] = = ')' :
print (right_bnum[ - 1 ], end = " " )
right_bnum.pop()
if __name__ = = "__main__" :
exp = "(a+(b*c))+(d/e)"
n = len (exp)
printBracketNumber(exp, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printBracketNumber( string exp,
int n)
{
int left_bnum = 1;
Stack< int > right_bnum = new Stack< int >();
for ( int i = 0; i < n; i++)
{
if (exp[i] == '(' )
{
Console.Write(left_bnum + " " );
right_bnum.Push(left_bnum);
left_bnum++;
}
else if (exp[i] == ')' )
{
Console.Write(right_bnum.Peek() + " " );
right_bnum.Pop();
}
}
}
static void Main()
{
string exp = "(a+(b*c))+(d/e)" ;
int n = exp.Length;
printBracketNumber(exp, n);
}
}
|
PHP
<?php
function printBracketNumber( $exp , $n )
{
$left_bnum = 1;
$right_bnum = array ();
$t = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $exp [ $i ] == '(' )
{
echo $left_bnum . " " ;
$right_bnum [ $t ++] = $left_bnum ;
$left_bnum ++;
}
else if ( $exp [ $i ] == ')' )
{
echo $right_bnum [ $t - 1] . " " ;
$right_bnum [ $t - 1] = 1;
$t --;
}
}
}
$exp = "(a+(b*c))+(d/e)" ;
$n = strlen ( $exp );
printBracketNumber( $exp , $n );
?>
|
Javascript
<script>
function printBracketNumber(exp, n)
{
var left_bnum = 1;
var right_bnum = [];
for ( var i = 0; i < n; i++) {
if (exp[i] == '(' ) {
document.write( left_bnum + " " );
right_bnum.push(left_bnum);
left_bnum++;
}
else if (exp[i] == ')' ) {
document.write( right_bnum[right_bnum.length-1] + " " );
right_bnum.pop();
}
}
}
var exp = "(a+(b*c))+(d/e)" ;
var n = exp.length;
printBracketNumber(exp, n);
</script>
|
Time Complexity : O(n).
Auxiliary Space : O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...