-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateBinaryStrings.java
More file actions
53 lines (45 loc) · 1.35 KB
/
GenerateBinaryStrings.java
File metadata and controls
53 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Created by MalhotR1 on 04/25/2017.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
public class GenerateBinaryStrings {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
for (int t = 0; t < T; t++) {
ArrayList<char[]> strings = new ArrayList<>();
char[] in = br.readLine().trim().toCharArray();
print(in, 0);
}
}
private static void print(char[] str, int index)
{
if (index == str.length)
{
printString(str);
return;
}
if (str[index] == '?')
{
// replace '?' by '0' and recurse
str[index] = '0';
print(str, index + 1);
// replace '?' by '1' and recurse
str[index] = '1';
print(str, index + 1);
// No need to backtrack as string is passed
// by value to the function
}
else
print(str, index + 1);
}
private static void printString(char[] in) {
for (int i = 0; i < in.length; i++) {
System.out.print(in[i]);
}
System.out.print(" ");
}
}