-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode360_GenerateBinaryStringWithoutConsecutive1.java
More file actions
84 lines (75 loc) · 2.64 KB
/
Code360_GenerateBinaryStringWithoutConsecutive1.java
File metadata and controls
84 lines (75 loc) · 2.64 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package recursion.subsequencepatterns;
import java.util.ArrayList;
import java.util.List;
public class Code360_GenerateBinaryStringWithoutConsecutive1 {
// Created at: 29-March-2025
// Last revised at: 29-March-2025
// Link:
// https://www.geeksforgeeks.org/generate-binary-strings-without-consecutive-1s/
// Time Complexity: O(F(N+2) * N) — F(N+2) strings, each of length N
// Space Complexity: O(N) — recursion depth
/*
* Problem Description:
* Given a positive integer N, generate all binary strings of length N
* that do not contain two consecutive 1s.
*
* Example:
* Input: N = 3
* Output: ["000", "001", "010", "100", "101"]
*
* Constraints:
* 1 <= N <= 20
*/
/*
* Approach: Recursion / Backtracking
*
* Idea:
* Build the string character by character from index 1 to N.
* At each position, '0' is always a valid choice.
* '1' is only valid if the previous character was not '1'.
* Track this with a boolean flag (prevIsOne).
*
* Time: O(F(N+2) * N) — the count of valid strings follows Fibonacci,
* and each string takes O(N) to build
* Space: O(N) — max recursion depth is N
*
* Key Insight:
* No visited array or set needed — every root-to-leaf path in the
* recursion tree produces a unique string by construction.
*/
/**
* Recursively builds all binary strings of the given length
* without consecutive 1s.
*
* @param ans list collecting valid strings
* @param len target length of each binary string
* @param i current position being filled (1-indexed)
* @param curr string built so far
* @param taken true if the previous character placed was '1'
*/
private void generate(List<String> ans, int len, int i, String curr, boolean taken) {
if (i > len) {
ans.add(curr);
return;
}
if (!taken) {
generate(ans, len, i + 1, curr + "0", false);
generate(ans, len, i + 1, curr + "1", true); // '1' only when prev was '0'
} else {
generate(ans, len, i + 1, curr + "0", false); // prev was '1', force '0'
}
}
/**
* Returns all binary strings of length N with no two consecutive 1s.
*
* @param N length of binary strings to generate
* @return list of valid binary strings, or empty list if N <= 0
*/
public List<String> generateString(int N) {
if (N <= 0)
return new ArrayList<>();
List<String> strs = new ArrayList<>();
generate(strs, N, 1, "", false);
return strs;
}
}