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.
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
- 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.
1 2 2 1 3 3
Time Complexity : O(n).
Auxiliary Space : O(n).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print the balanced bracket expression using given brackets
- Print all ways to break a string in bracket form
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find index of closing bracket for a given opening bracket in an expression
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Number of balanced bracket subsequence of length 2 and 4
- Number of closing brackets needed to complete a regular bracket sequence
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Expression contains redundant bracket or not
- Check if expression contains redundant bracket or not | Set 2
- Minimum Swaps for Bracket Balancing
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Range Queries for Longest Correct Bracket Subsequence
- Construct Binary Tree from String with bracket representation
- Print a number strictly less than a given number such that all its digits are distinct.
- Print all permutations of a number N greater than itself
- Print all the possible arithmetic expressions for a given number
- Print Nth Stepping or Autobiographical number
- Print all Prime Quadruplet of a number less than it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.