title: Understand the code for generation of all De Bruijn sequences
body: <p>I have some questions regarding <a href="https://math.stackexchange.com/a/1377263/511447">generation of all De Bruijn sequences</a></p>
  <p>What is the purpose of the condition : <code>2*arrange.count(0) == 2**N</code> and one other condition: <code>if not new_seen[seen_i]</code> ?</p>
  <p>How do these conditions count equal zeros and ones and the other is a basic depth-first-search condition to recurse ?</p>
  <pre><code># De Bruijn sequences return...
  # Generate strings using DFS, ignoring strings with &quot;seen&quot; subsequences
  
  def S(arrange, seen, N):
      # len(arrange) can be removed if 00000 is marked as seen
      if len(arrange) == 2**N and 2*arrange.count(0) == 2**N:
          print(''.join(map(str, arrange)))
  
      for c in (0, 1):
          new_seen = seen[:]
          new_arrange = arrange + [c]
  
          word = ''.join(map(str, new_arrange[-N:]))
          seen_i = int(word, 2)
  
          if not new_seen[seen_i]:
              new_seen[seen_i] = 1
              S(new_arrange, new_seen, N)
  
  
  S([0]*5, [0]*2**5, 5)  # Beginning must be 00000
  </code></pre>