  Motivated by applications in DNA-based data storage, constrained codes have
attracted a considerable amount of attention from both academia and industry.
We study the maximum cardinality of constrained codes for which the constraints
can be characterized by a set of forbidden substrings, where by a substring we
mean some consecutive coordinates in a string.
  For finite-type constrained codes (for which the set of forbidden substrings
is finite), one can compute their capacity (code rate) by the ``spectral
method'', i.e., by applying the Perron-Frobenious theorem to the de Brujin
graph defined by the code. However, there was no systematic method to compute
the exact cardinality of these codes.
  We show that there is a surprisingly powerful method arising from enumerative
combinatorics, which is based on the Goulden-Jackson cluster theorem
(previously not known to the coding community), that can be used to compute not
only the capacity, but also the exact formula for the cardinality of these
codes, for each fixed code length. Moreover, this can be done by solving a
system of linear equations of size equal to the number of constraints.
  We also show that the spectral method and the cluster method are inherently
related by establishing a direct connection between the spectral radius of the
de Brujin graph used in the first method and the convergence radius of the
generating function used in the second method.
  Lastly, to demonstrate the flexibility of the new method, we use it to give
an explicit upper bound on the maximum cardinality of variable-length
non-overlapping codes, which are a class of constrained codes defined by an
infinite number of forbidden substrings.
