|
| 1 | +--- |
| 2 | +id: generate-binary-strings-without-adjacent-zeros |
| 3 | +title: 3211. Generate Binary Strings Without Adjacent Zeros |
| 4 | +sidebar_label: 3211. Generate Binary Strings Without Adjacent Zeros |
| 5 | +tags: |
| 6 | +- String |
| 7 | +- Bit Manipulation |
| 8 | +- Recursion |
| 9 | + |
| 10 | +description: "This is a solution to the 3211. Generate Binary Strings Without Adjacent Zeros." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | +You are given a positive integer n. |
| 15 | +A binary string x is valid if all |
| 16 | +substrings |
| 17 | + of x of length 2 contain at least one "1". |
| 18 | + |
| 19 | +Return all valid strings with length n, in any order. |
| 20 | + ### Examples |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: n = 3 |
| 24 | +
|
| 25 | +Output: ["010","011","101","110","111"] |
| 26 | +
|
| 27 | +Explanation: |
| 28 | +
|
| 29 | +The valid strings of length 3 are: "010", "011", "101", "110", and "111". |
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | +``` |
| 34 | +Input: n = 1 |
| 35 | +
|
| 36 | +Output: ["0","1"] |
| 37 | +
|
| 38 | +Explanation: |
| 39 | +
|
| 40 | +The valid strings of length 1 are: "0" and "1". |
| 41 | +``` |
| 42 | + |
| 43 | +### Constraints |
| 44 | +- `1 <= n <= 18` |
| 45 | +## Solution for 3211. Generate Binary Strings Without Adjacent Zeros |
| 46 | + |
| 47 | +To solve this problem, we can use DFS to check recursively all possible solutions. |
| 48 | + |
| 49 | +## Approach |
| 50 | + |
| 51 | +1. Start with an empty string and begin at position `i = 0`. |
| 52 | +2. For each position `i` in the binary string of length `n`, consider two possible values: `0` and `1`. |
| 53 | +3. |
| 54 | +3.1. If you choose `0`, ensure the previous character (at position `i-1`) is `1` to maintain validity (i.e., avoid consecutive 00). |
| 55 | +3.2. If valid, recursively move to the next position. |
| 56 | +4. If you choose `1`, it’s always valid. Recursively move to the next position. |
| 57 | +5. When you reach the end of the string (length `n`), add the valid string to the result. |
| 58 | +6. Continue until all valid strings of length `n` are generated. |
| 59 | +7. Collect and return all the valid strings. |
| 60 | + |
| 61 | +### Code in Different Languages |
| 62 | + |
| 63 | +<Tabs> |
| 64 | +<TabItem value="C++" label="C++" default> |
| 65 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 66 | + |
| 67 | +```cpp |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + vector<string> validStrings(int n) { |
| 71 | + vector<string> ans; |
| 72 | + string t; |
| 73 | + auto dfs = [&](auto&& dfs, int i) { |
| 74 | + if (i >= n) { |
| 75 | + ans.emplace_back(t); |
| 76 | + return; |
| 77 | + } |
| 78 | + for (int j = 0; j < 2; ++j) { |
| 79 | + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { |
| 80 | + t.push_back('0' + j); |
| 81 | + dfs(dfs, i + 1); |
| 82 | + t.pop_back(); |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + dfs(dfs, 0); |
| 87 | + return ans; |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +</TabItem> |
| 92 | +<TabItem value="Java" label="Java"> |
| 93 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + private List<String> ans = new ArrayList<>(); |
| 97 | + private StringBuilder t = new StringBuilder(); |
| 98 | + private int n; |
| 99 | +
|
| 100 | + public List<String> validStrings(int n) { |
| 101 | + this.n = n; |
| 102 | + dfs(0); |
| 103 | + return ans; |
| 104 | + } |
| 105 | +
|
| 106 | + private void dfs(int i) { |
| 107 | + if (i >= n) { |
| 108 | + ans.add(t.toString()); |
| 109 | + return; |
| 110 | + } |
| 111 | + for (int j = 0; j < 2; ++j) { |
| 112 | + if ((j == 0 && (i == 0 || t.charAt(i - 1) == '1')) || j == 1) { |
| 113 | + t.append(j); |
| 114 | + dfs(i + 1); |
| 115 | + t.deleteCharAt(t.length() - 1); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +</TabItem> |
| 123 | + |
| 124 | +<TabItem value="Python" label="Python"> |
| 125 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 126 | + |
| 127 | +```python |
| 128 | +class Solution: |
| 129 | + def validStrings(self, n: int) -> List[str]: |
| 130 | + def dfs(i: int): |
| 131 | + if i >= n: |
| 132 | + ans.append("".join(t)) |
| 133 | + return |
| 134 | + for j in range(2): |
| 135 | + if (j == 0 and (i == 0 or t[i - 1] == "1")) or j == 1: |
| 136 | + t.append(str(j)) |
| 137 | + dfs(i + 1) |
| 138 | + t.pop() |
| 139 | + |
| 140 | + ans = [] |
| 141 | + t = [] |
| 142 | + dfs(0) |
| 143 | + return ans |
| 144 | +``` |
| 145 | + |
| 146 | +</TabItem> |
| 147 | +</Tabs> |
| 148 | + |
| 149 | +#### Complexity Analysis |
| 150 | + |
| 151 | +- **Time Complexity**: The time complexity is $O(n \times 2^n)$ , where n is the length of the string. |
| 152 | +- **Space Complexity**: Ignoring the space consumption of the answer array, the space complexity is $O(n)$ . |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +<h2>Authors:</h2> |
| 157 | + |
| 158 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 159 | +{['nagalakshmi08'].map(username => ( |
| 160 | + <Author key={username} username={username} /> |
| 161 | +))} |
| 162 | +</div> |
0 commit comments