Print all Balanced Brackets Strings that can be formed by replacing wild card ‘?’
Given string str containing characters ‘?’, ‘(‘ and ‘)’, the task is to replace the ‘?’ character with ‘(‘ or ‘)’ and print all the strings containing balanced brackets
Example:
Input: str = “????”Output: ()() (())
Input: str = “(()?”Output: (())
Approach: The given problem can be solved using recursion and backtracking . The idea is to substitute every ‘?’ character with ‘)’ then make a recursive call to the next index and after backtracking change it to ‘(‘ then make a recursive call to the next index, after backtracking change the character back to ‘?’ . Follow the steps below to solve the problem:
Convert the string str into a character array , say ch
Pass the character array ch and index 0 as parameters inside recursive function and at every recursive call perform the following:
If the index becomes equal to the length of the character array the:
If the current character ch[index] is either ‘(‘ or ‘)’ then make a recursive call on the next index
If the current character ch[index] is ‘?’ then:
Replace it with ‘(‘ and make a recursive call on the next index
Replace it with ‘)’ and make a recursive call on the next index
Change it back to ‘?’ before returning from the function
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print(string ch)
{
for (char c : ch) {
cout << c;
}
cout << endl;
}
bool check(string ch)
{
stack<char> S;
if (ch[0] == ')') {
return false;
}
for (int i = 0; i < ch.length(); i++) {
if (ch[i] == '(') {
S.push('(');
}
else {
if (S.size() == 0)
return false;
else
S.pop();
}
}
if (S.size() == 0)
return true;
else
return false;
}
void count(string ch, int index)
{
if (index == ch.length()) {
if (check(ch)) {
print(ch);
}
return;
}
if (ch[index] == '?') {
ch[index] = '(';
count(ch, index + 1);
ch[index] = ')';
count(ch, index + 1);
ch[index] = '?';
}
else {
count(ch, index + 1);
}
}
int main()
{
string ch = "????";
count(ch, 0);
return 0;
}
Java
import java.io.*;
import java.util.*;
class Main {
static void print(char ch[])
{
for (Character c : ch) {
System.out.print(c);
}
System.out.println();
}
static boolean check(char ch[])
{
Stack<Character> S = new Stack<>();
if (ch[0] == ')') {
return false;
}
for (int i = 0; i < ch.length; i++) {
if (ch[i] == '(') {
S.add('(');
}
else {
if (S.size() == 0)
return false;
else
S.pop();
}
}
if (S.size() == 0)
return true;
else
return false;
}
static void count(char ch[], int index)
{
if (index == ch.length) {
if (check(ch)) {
print(ch);
}
return;
}
if (ch[index] == '?') {
ch[index] = '(';
count(ch, index + 1);
ch[index] = ')';
count(ch, index + 1);
ch[index] = '?';
}
else {
count(ch, index + 1);
}
}
public static void main(String[] args)
{
String m = "????";
char ch[] = m.toCharArray();
count(ch, 0);
}
}
Python3
def printf(ch):
for c in ch:
print(c, end="");
print("");
def check(ch):
S = [];
if (ch[0] == ')'):
return False;
for i in range(len(ch)):
if (ch[i] == '('):
S.append('(');
else:
if (len(S) == 0):
return False;
else:
S.pop();
if (len(S) == 0):
return True;
else:
return False;
def count(ch, index):
if (index == len(ch)):
if (check(ch)):
printf(ch);
return;
if (ch[index] == '?'):
ch[index] = '(';
count(ch, index + 1);
ch[index] = ')';
count(ch, index + 1);
ch[index] = '?';
else:
count(ch, index + 1);
ch = "????";
count(list(ch), 0);
C#
using System;
using System.Collections;
public class Gfg{
static void print(char []ch)
{
foreach (char c in ch) {
Console.Write(c);
}
Console.WriteLine();
}
static bool check(char []ch)
{
Stack S = new Stack();
if (ch[0] == ')') {
return false;
}
for (int i = 0; i < ch.Length; i++) {
if (ch[i] == '(') {
S.Push('(');
}
else {
if (S.Count == 0)
return false;
else
S.Pop();
}
}
if (S.Count == 0)
return true;
else
return false;
}
static void count(char []ch, int index)
{
if (index == ch.Length) {
if (check(ch)) {
print(ch);
}
return;
}
if (ch[index] == '?') {
ch[index] = '(';
count(ch, index + 1);
ch[index] = ')';
count(ch, index + 1);
ch[index] = '?';
}
else {
count(ch, index + 1);
}
}
public static void Main(string[] args)
{
string m = "????";
char []ch = m.ToCharArray();
count(ch, 0);
}
}
Javascript
<script>
function printf(ch) {
for (c of ch) {
document.write(c);
}
document.write("<br>");
}
function check(ch) {
let S = [];
if (ch[0] == ')') {
return false;
}
for (let i = 0; i < ch.length; i++) {
if (ch[i] == '(') {
S.push('(');
}
else {
if (S.length == 0)
return false;
else
S.pop();
}
}
if (S.length == 0)
return true;
else
return false;
}
function count(ch, index) {
if (index == ch.length) {
if (check(ch)) {
printf(ch);
}
return;
}
if (ch[index] == '?') {
ch[index] = '(';
count(ch, index + 1);
ch[index] = ')';
count(ch, index + 1);
ch[index] = '?';
}
else {
count(ch, index + 1);
}
}
let ch = "????";
count(ch.split(""), 0);
</script>
Time Complexity: O(N*2^N)Auxiliary Space: O(N)
Last Updated :
12 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...