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).
- 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
- Minimum Swaps for Bracket Balancing
- Check if expression contains redundant bracket or not | Set 2
- Print a number strictly less than a given number such that all its digits are distinct.
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Range Queries for Longest Correct Bracket Subsequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.