Given four integers a, b, c and d which signifies the number of four types of brackets.
The task is to print any balanced bracket expression using all the given brackets. If we cannot form a balanced bracket expression then print -1. In case of multiple answers, print any one.
Input: a = 3, b = 1, c = 4, d = 3
Input: a = 3, b = 1, c = 4, d = 8
No balanced bracket expression is possible with the given brackets.
Approach: First check if the balanced bracket expression can be formed with the given number of brackets. We can form the expression if the number of type1 brackets is equal to the number of type4 brackets with any number of type3 brackets or if there are only type 2 brackets. Hence the combining condition will be:
(a == d && a) || (a == 0 && c == 0 && d == 0)
The following steps can be followed to print the balanced bracket expression if the above condition is satisfied:
- Print number of type1 brackets.
- Print number of type3 brackets.
- Print number of type4 brackets.
- Print number of type2 brackets.
Below is the implementation of the above approach:
- Find the lexicographical next balanced bracket sequence
- Number of balanced bracket subsequence of length 2 and 4
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Convert an unbalanced bracket sequence to a balanced sequence
- Print Bracket Number
- Print all combinations of balanced parentheses
- Balance a string after removing extra brackets
- Balanced Prime
- Maximize the value of the given expression
- Expression Evaluation
- Number of balanced parenthesis substrings
- Pairs involved in Balanced Parentheses
- Check if concatenation of two strings is balanced or not
- What is an Expression and What are the types of Expressions?
- Find all possible outcomes of a given expression
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.