-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_binary_strings_without_adjacent_zeros.py
62 lines (39 loc) · 1.28 KB
/
generate_binary_strings_without_adjacent_zeros.py
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
"""
You are given a positive integer n.
A binary string x is valid if all
substrings
of x of length 2 contain at least one "1".
Return all valid strings with length n, in any order.
Example 1:
Input: n = 3
Output: ["010","011","101","110","111"]
Explanation:
The valid strings of length 3 are: "010", "011", "101", "110", and "111".
Example 2:
Input: n = 1
Output: ["0","1"]
Explanation:
The valid strings of length 1 are: "0" and "1".
Constraints:
1 <= n <= 18
"""
class Solution:
def validStrings(self, n: int) -> List[str]:
def dfs(n, current_string):
# Base Case
if len(current_string) == n:
all_valid_strings.append(current_string)
return
# Recursive Case
# If the last char is '0', only append '1' to avoid "00"
if current_string and current_string[-1] == "0":
dfs(n, current_string + "1")
else:
dfs(n, current_string + "0")
dfs(n, current_string + "1")
# Initialize the list to store all valid strings
all_valid_strings = []
# start the recursion with an empty string
dfs(n, "")
# return the list of all valid strings
return all_valid_strings