{"id":742,"date":"2025-07-22T13:10:56","date_gmt":"2025-07-22T07:40:56","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=742"},"modified":"2025-07-22T13:10:58","modified_gmt":"2025-07-22T07:40:58","slug":"generate-all-binary-strings","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/","title":{"rendered":"Generate All Binary Strings | Backtracking Approach"},"content":{"rendered":"\n<p>Given an integer&nbsp;<strong>N<\/strong>&nbsp;, Print all binary strings of size N&nbsp;which do not contain&nbsp;consecutive 1s.<\/p>\n\n\n\n<p>A binary string is that string which contains only 0 and 1.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/generate-all-binary-strings\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Example 1:<\/strong><br><strong>Input:<\/strong><br>N = 3<br><strong>Output:<\/strong><br>000 , 001 , 010 , 100 , 101<br><strong>Explanation:<\/strong><br>None of the above strings contain consecutive 1s. \"110\" is not an answer as it has '1's occuring consecutively. <\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><\/p>\n\n\n\n<p>You don&#8217;t need to read input or print anything. Your task is to complete the function<strong>&nbsp;generateBinaryStrings()<\/strong>&nbsp;which takes an integer&nbsp;<strong>N&nbsp;<\/strong>as input and&nbsp;returns a list of all valid binary strings in lexicographically increasing order.<\/p>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>&nbsp;O(2<sup>N<\/sup>)<br><strong>Expected Auxiliary Space:<\/strong>&nbsp;O(N)<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><br>1 &lt;= N &lt;= 20<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-d4cddb43-380b-46f6-b6a1-cafa78ad1be1\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#0-solution-backtracking-approach\" style=\"\">Solution: Backtracking Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/generate-all-binary-strings\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-backtracking-approach\">Solution: Backtracking Approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like filling positions in a string one by one, but with a rule: &#8220;You can&#8217;t place two 1&#8217;s next to each other!&#8221; It&#8217;s like a game where you&#8217;re placing 0&#8217;s and 1&#8217;s, but whenever you place a 1, the next position becomes &#8220;dangerous&#8221; &#8211; you can only put a 0 there. When you place a 0, the next position becomes &#8220;safe&#8221; again &#8211; you can put either 0 or 1. We use backtracking to explore all valid combinations by trying different choices and undoing them when needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize Setup<\/strong>: Create an array of &#8220;0&#8221;s of length N to represent positions in the binary string.<\/li>\n\n\n\n<li><strong>Recursive Function<\/strong>: Use a helper function that takes the current index, a flag indicating if we can place &#8216;1&#8217;, the current string array, and result list.<\/li>\n\n\n\n<li><strong>Base Case<\/strong>: When index reaches N, we&#8217;ve filled all positions &#8211; add the current string to results.<\/li>\n\n\n\n<li><strong>Always Try &#8216;0&#8217;<\/strong>: At any position, we can always place &#8216;0&#8217; (it doesn&#8217;t violate the no-consecutive-1&#8217;s rule).<\/li>\n\n\n\n<li><strong>Conditionally Try &#8216;1&#8217;<\/strong>: Only place &#8216;1&#8217; if the flag is True (meaning the previous character wasn&#8217;t &#8216;1&#8217;).<\/li>\n\n\n\n<li><strong>Update Flag<\/strong>: After placing &#8216;0&#8217;, set flag=True for next position. After placing &#8216;1&#8217;, set flag=False for next position.<\/li>\n\n\n\n<li><strong>Backtrack<\/strong>: After exploring one choice, reset the position and try other possibilities.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def solve(self, index, flag, numbers, result):\n        # Base case: If we've filled all positions, add to result\n        if index &gt;= len(numbers):\n            result.append(&quot;&quot;.join(numbers))  # Convert array to string\n            return\n        \n        # Choice 1: Always place '0' at current position\n        numbers[index] = &quot;0&quot;\n        self.solve(index + 1, True, numbers, result)  # Next position can have 0 or 1\n        \n        # Choice 2: Place '1' only if flag is True (no consecutive 1's)\n        if flag == True:\n            numbers[index] = &quot;1&quot;\n            self.solve(index + 1, False, numbers, result)  # Next position can only have 0\n            numbers[index] = &quot;0&quot;  # Backtrack: reset to &quot;0&quot; for clean slate\n\n    def generateBinaryStrings(self, n):\n        numbers = [&quot;0&quot;] * n  # Initialize array with all &quot;0&quot;s\n        result = []          # List to store all valid binary strings\n        self.solve(0, True, numbers, result)  # Start from index 0, flag=True\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">solve<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">flag<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">numbers<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">result<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base case: If we&#39;ve filled all positions, add to result<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> index &gt;= <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(numbers):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            result.append(<\/span><span style=\"color: #CE9178\">&quot;&quot;<\/span><span style=\"color: #D4D4D4\">.join(numbers))  <\/span><span style=\"color: #6A9955\"># Convert array to string<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Choice 1: Always place &#39;0&#39; at current position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        numbers[index] = <\/span><span style=\"color: #CE9178\">&quot;0&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(index + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">, numbers, result)  <\/span><span style=\"color: #6A9955\"># Next position can have 0 or 1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Choice 2: Place &#39;1&#39; only if flag is True (no consecutive 1&#39;s)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> flag == <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            numbers[index] = <\/span><span style=\"color: #CE9178\">&quot;1&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(index + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">, numbers, result)  <\/span><span style=\"color: #6A9955\"># Next position can only have 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            numbers[index] = <\/span><span style=\"color: #CE9178\">&quot;0&quot;<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Backtrack: reset to &quot;0&quot; for clean slate<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">generateBinaryStrings<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        numbers = [<\/span><span style=\"color: #CE9178\">&quot;0&quot;<\/span><span style=\"color: #D4D4D4\">] * n  <\/span><span style=\"color: #6A9955\"># Initialize array with all &quot;0&quot;s<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []          <\/span><span style=\"color: #6A9955\"># List to store all valid binary strings<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">, numbers, result)  <\/span><span style=\"color: #6A9955\"># Start from index 0, flag=True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>The&nbsp;<code>solve<\/code>&nbsp;function builds binary strings recursively while maintaining the no-consecutive-1&#8217;s constraint using the&nbsp;<code>flag<\/code>&nbsp;parameter. At each position, it first tries placing &#8216;0&#8217; (always allowed) and recurses with flag=True, meaning the next position can have either 0 or 1. Then, if the flag is True (previous character wasn&#8217;t &#8216;1&#8217;), it tries placing &#8216;1&#8217; and recurses with flag=False, meaning the next position can only have &#8216;0&#8217;. The backtrack step&nbsp;<code>numbers[index] = \"0\"<\/code>&nbsp;ensures we clean up after trying &#8216;1&#8217; so other branches aren&#8217;t affected. When we reach the end, we join the array into a string and add it to results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through N=3:<\/p>\n\n\n\n<p><strong>Start: index=0, flag=True, numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;]<\/strong><\/p>\n\n\n\n<p><strong>Branch 1: Place &#8216;0&#8217; at index 0<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], recurse with index=1, flag=True<strong>Branch 1.1: Place &#8216;0&#8217; at index 1<\/strong><ul><li>numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], recurse with index=2, flag=True<strong>Branch 1.1.1: Place &#8216;0&#8217; at index 2<\/strong><ul><li>numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], index=3 \u2192 add &#8220;000&#8221; to result<\/li><\/ul><strong>Branch 1.1.2: Place &#8216;1&#8217; at index 2 (flag=True)<\/strong><ul><li>numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;1&#8221;], index=3 \u2192 add &#8220;001&#8221; to result<\/li><li>Backtrack: numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;]<\/li><\/ul><\/li><\/ul><strong>Branch 1.2: Place &#8216;1&#8217; at index 1 (flag=True)<\/strong>\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;0&#8243;,&#8221;1&#8243;,&#8221;0&#8221;], recurse with index=2, flag=False<strong>Branch 1.2.1: Place &#8216;0&#8217; at index 2 only (flag=False)<\/strong>\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;0&#8243;,&#8221;1&#8243;,&#8221;0&#8221;], index=3 \u2192 add &#8220;010&#8221; to result<\/li>\n\n\n\n<li>Backtrack: numbers=[&#8220;0&#8243;,&#8221;0&#8243;,&#8221;0&#8221;]<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Branch 2: Place &#8216;1&#8217; at index 0 (flag=True)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;1&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], recurse with index=1, flag=False<strong>Branch 2.1: Place &#8216;0&#8217; at index 1 only (flag=False)<\/strong>\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;1&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], recurse with index=2, flag=True<strong>Branch 2.1.1: Place &#8216;0&#8217; at index 2<\/strong><ul><li>numbers=[&#8220;1&#8243;,&#8221;0&#8243;,&#8221;0&#8221;], index=3 \u2192 add &#8220;100&#8221; to result<\/li><\/ul><strong>Branch 2.1.2: Place &#8216;1&#8217; at index 2 (flag=True)<\/strong>\n<ul class=\"wp-block-list\">\n<li>numbers=[&#8220;1&#8243;,&#8221;0&#8243;,&#8221;1&#8221;], index=3 \u2192 add &#8220;101&#8221; to result<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>[\"000\", \"001\", \"010\", \"100\", \"101\"]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(2^n) &#8211; In the worst case, we might explore up to 2^n combinations, though the constraint reduces the actual number significantly.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(n) &#8211; The recursion stack depth is n, plus space for the numbers array.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h2>\n\n\n\n<p>This backtracking approach is like playing a careful game where you fill positions one by one with a safety rule. The&nbsp;<code>flag<\/code>&nbsp;acts like a traffic light: green (True) means you can place either 0 or 1, red (False) means you can only place 0. Every time you place a 1, you turn the next light red to prevent consecutive 1&#8217;s. The backtracking ensures you try all valid combinations systematically, and the recursive structure naturally generates all possibilities without missing any or creating duplicates.<\/p>\n\n\n\n<p>The key insight is using the flag to track the constraint state, making it easy to enforce the &#8220;no consecutive 1&#8217;s&#8221; rule without complex checking logic. This pattern works great for constraint-based string generation problems!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;N&nbsp;, Print all binary strings of size N&nbsp;which do not contain&nbsp;consecutive 1s. A binary string is that string which contains only 0 and 1. Here&#8217;s the [Problem Link] to begin with. Example 1:Input:N = 3Output:000 , 001 , 010 , 100 , 101Explanation:None of the above strings contain consecutive 1s. &#8220;110&#8221; is not<\/p>\n","protected":false},"author":1,"featured_media":743,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[32,19],"class_list":{"0":"post-742","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-advance-recursion","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/generate-all-binary-strings-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":744,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/742\/revisions\/744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/743"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}