- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all Balanced Brackets Strings that can be formed by replacing wild card β?β
Balanced Brackets mean if we have a string of brackets then each open bracket has a corresponding close bracket and the pair of brackets is properly nested. The size of the string should be ab even number. In this problem we have given a string of the brackets that also contain the character β?β and our task is to form every possible balanced bracket string by replacing the β?β to appropriate brackets. In our given string only parentheses β(β and β)β brackets are used.
Sample Examples
Input 1: str = β()(?)?β Output 1: ()(())
Explanation
Only one balanced string is possible to form by replacing β?β.
Input 2: str = β??????β
Output 2: ((())) (()()) (())() ()(()) ()()()
Explanation
There are two possible ways to form a balanced string.
One way is to replace indexes 0, 1, and 2 with an open bracket and the other indexes with a closed bracket.
The second way is to replace indexes 0, 1, and 3 with an open bracket and other indexes with a closed bracket.
Third way is to replace indexes 0, 1, and 4 with an open bracket and other indexes with a closed bracket.
Fourth way is to replace indexes 0, 2, and 3 with an open bracket and other indexes with a closed bracket.
The last way is to replace indexes 0, 2, and 4 with an open bracket and other indexes with a closed bracket.
Approach
We have seen the example above for the given string, let us move to the approach β
We can solve this problem using a backtracking approach.
Letβs discuss this approach below β
First, we will initialize a function βcreateβ to create all possible strings after replacing β?β with the bracket with parameters str and index = 0.
In the function,
Initializing the βcheckβ function to verify whether the string is balanced or not.
β> First, we set the base condition. If we reached the end point of the string then we have to pass that string to the βcheckβ function to verify the string whether is balanced or not. If it is balanced then print the string.
β>If the current character of the string is β?β,
First, replace it with the open bracket and call the same function to check whether we reached the end of the string or not.
Secondly, replace it with the close bracket and again call the same function to check whether we reached the end of the string or not.
In the end, we backtrack the string and assign the current character β?β
β> Else, the current character of the string is a bracket then move to the next index by calling the same function.
β> In this function we initialize the stack and then check
β> If the first character of the string is a close bracket, then return false
β> Else if the current bracket is closed, further there are two conditions If the stack is empty return false as there is in the corresponding open bracket. Else pop the corresponding open bracket from the stack.
β> In the end, we have checked If the stack is empty means the string is balanced and returns true else there are some brackets left means the string is not balanced and returns false.
Example
Below is C++ code for the above approach of the backtracking to get the all the balanced strings
#include <bits/stdc++.h> using namespace std; // Function 'check' to verify whether the string is balanced or not bool check(string str){ stack<char> S; // created stack // If the first character of the string is a close bracket, then return false if (str[0] == ')') { return false; } // Traverse the string using for loop for (int i = 0; i < str.size(); i++) { // If the current character is an open bracket, then push it into the stack if (str[i] == '(') { S.push('('); } // If the current character is a close bracket else { // If the stack is empty, there is no corresponding open bracket return false if (S.empty()){ return false; } // Else pop the corresponding opening bracket from the stack else S.pop(); } } // If the stack is empty, return true if (S.empty()){ return true; } else { return false; } } // Function 'create' to create all possible bracket strings void create(string str, int i){ // If reached the end of the string if (i == str.size()) { // passed 'str' to the 'check' function to verify whether the string is balanced or not if (check(str)) { // If it is a balanced string cout<< str << endl; // print the string } return; } // If the current character of the string is '?' if (str[i] == '?') { str[i] = '('; // replace ? with ( create(str, i + 1); // continue to next character str[i] = ')'; // replace ? with ) create(str, i + 1); // continue to next character // backtrack str[i] = '?'; } // If the current character is bracketed then move to the next index else { create(str, i + 1); } } int main(){ string str = "??????"; //given string // Call the function create (str, 0); return 0; }
Output
((())) (()()) (())() ()(()) ()()()
Time and Space Complexity
The time complexity of the above code is O(N*(2^N), as we have to backtrack over the string.
The space complexity of the above code is O(N), as we are storing the brackets in the stack.
Where N is the size of the string.
Conclusion
In this tutorial, we have implemented a program to Print all Balanced Brackets Strings that can be formed by replacing wild card β?β. We have implemented an approach of Backtracking. The time complexity is O(N*(2^N) and the space complexity of O(N). Where N is the size of the string.