generate_binary_strings_without_adjacent_zeros

 1# @leet start
 2class Solution:
 3    def validStrings(self, n: int) -> list[str]:
 4        """
 5        This code manually generates all the binary strings for numbers from 0..2^n
 6        And then makes sure there's at least one 1 every two digits.
 7
 8        To generate the strings properly, since they have to be padded properly,
 9        the format function is used with the string f"#0{n + 2}b".
10        This guarantees that the generated string is left padded with enough zeroes.
11        Finally, the validate function checks to make sure there's at least one 1 every two digits.
12        """
13        strs = [format(i, f"#0{n + 2}b")[2:] for i in range(2**n)]
14
15        def validate(s):
16            prev_one = True
17            for c in s:
18                if not prev_one and c == "0":
19                    return False
20                prev_one = c == "1"
21            return True
22
23        return [s for s in strs if validate(s) if len(s) == n]
24
25
26# @leet end
27def test():
28    pass
class Solution:
 3class Solution:
 4    def validStrings(self, n: int) -> list[str]:
 5        """
 6        This code manually generates all the binary strings for numbers from 0..2^n
 7        And then makes sure there's at least one 1 every two digits.
 8
 9        To generate the strings properly, since they have to be padded properly,
10        the format function is used with the string f"#0{n + 2}b".
11        This guarantees that the generated string is left padded with enough zeroes.
12        Finally, the validate function checks to make sure there's at least one 1 every two digits.
13        """
14        strs = [format(i, f"#0{n + 2}b")[2:] for i in range(2**n)]
15
16        def validate(s):
17            prev_one = True
18            for c in s:
19                if not prev_one and c == "0":
20                    return False
21                prev_one = c == "1"
22            return True
23
24        return [s for s in strs if validate(s) if len(s) == n]
def validStrings(self, n: int) -> list[str]:
 4    def validStrings(self, n: int) -> list[str]:
 5        """
 6        This code manually generates all the binary strings for numbers from 0..2^n
 7        And then makes sure there's at least one 1 every two digits.
 8
 9        To generate the strings properly, since they have to be padded properly,
10        the format function is used with the string f"#0{n + 2}b".
11        This guarantees that the generated string is left padded with enough zeroes.
12        Finally, the validate function checks to make sure there's at least one 1 every two digits.
13        """
14        strs = [format(i, f"#0{n + 2}b")[2:] for i in range(2**n)]
15
16        def validate(s):
17            prev_one = True
18            for c in s:
19                if not prev_one and c == "0":
20                    return False
21                prev_one = c == "1"
22            return True
23
24        return [s for s in strs if validate(s) if len(s) == n]

This code manually generates all the binary strings for numbers from 0..2^n And then makes sure there's at least one 1 every two digits.

To generate the strings properly, since they have to be padded properly, the format function is used with the string f"#0{n + 2}b". This guarantees that the generated string is left padded with enough zeroes. Finally, the validate function checks to make sure there's at least one 1 every two digits.

def test():
28def test():
29    pass