Restricted-growth string (source code)

= Restricted-growth string

A <restricted-growth string> encodes a partition of the ordered label set $\{1,\ldots,n\}$ by numbering blocks in the order in which they first appear. For a nonempty string $r_1,\ldots,r_n$, the rule is
$$
r_1=1,\qquad 1\leq r_i\leq1+\max_{j<i}r_j\quad(i>1).
$$
Equal entries identify elements in the same block. For example, $12133$ encodes $\{\{1,3\},\{2\},\{4,5\}\}$. At the next position one may reuse an existing block number or introduce exactly the next unused number; skipping a new number would break the convention.

Every <set partition> has one such canonical encoding, so these strings enumerate <set partition>[set partitions] without repeatedly counting permutations of the block names. Their number is the <Bell number> $B_n$, and the number with exactly $k$ distinct labels is the <Stirling number of the second kind> $S(n,k)$.