• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 04, 2022 |1.5K Views
Print Bracket Number
  Share   Like
Description
Discussion

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

 

  1. Define a variable left_bnum = 1.
  2. Create a stack right_bnum.
  3. Now, for i = 0 to n-1.
    1. If exp[i] == ‘(‘, then print left_bnum, push left_bnum on to the stack right_bnum and finally increment left_bnum by 1.
    2. Else if exp[i] == ‘)’, then print the top element of the stack right_bnum and then pop the top element from the stack.


Print Bracket Number  : https://www.geeksforgeeks.org/print-bracket-number/

Read More