Maths & numbers

Pattern avoidance word count calculator with automaton

Counting words that avoid forbidden substrings requires remembering a relevant suffix of each partial word.

Your numbers

Two to eight distinct single ASCII letters or digits.
One to twelve words, separated by spaces or commas; each at most 12 characters.
Integer 0–500. Includes any fixed prefix.
Leave blank for no fixed prefix. Uses alphabet characters only.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Counting words that avoid forbidden substrings requires remembering a relevant suffix of each partial word. This calculator builds a finite automaton from proper prefixes of the forbidden patterns, then counts all allowed continuations exactly. Overlapping patterns are handled together. An optional fixed prefix conditions the count and probability on that exact beginning rather than silently counting the full unconditioned alphabet space.

THE FORMULADP_(t+1)(q′)+=DP_t(q) for each nonforbidden automaton transition q --symbol→ q′.

A worked example

Eight binary words of length four avoid 11: 0000,0001,0010,0100,0101,1000,1001 and 1010. The automaton needs only the empty suffix and suffix 1. From suffix 1, appending another 1 is blocked. With no fixed prefix, the probability is 8/16=1/2. A fixed prefix 11 gives zero allowed completions before any remaining symbols are chosen.

Good to know

The alphabet contains two to eight distinct single ASCII letters or digits. Supply one to twelve nonempty forbidden words, length at most twelve, and total word length zero to five hundred. At most one hundred proper-prefix states are supported. The prefix may be blank and cannot exceed the total length. Characters are independent and uniform; regular expressions and weighted alphabets are unsupported.

Remember only suffixes that can begin a forbidden word

After reading a partial word, a long past prefix is irrelevant unless one of its suffixes matches the beginning of a forbidden pattern. The automaton state is the longest such proper prefix. Appending one character either completes a forbidden pattern and blocks the transition, or moves to the longest relevant suffix of the extended word.

This fallback rule handles overlapping patterns. For instance, avoiding 101 must still remember a trailing 1 after reading 11, because that final character could start a future 101. Dropping the state to empty after every mismatch would miss such occurrences. The transition table exposes every state and destination so the remembered information can be inspected.

Avoiding consecutive ones needs two allowed suffix states and one blocked transitionEmpty suffixSuffix 110 returns to empty1BlockedLength 4: 8 allowed / 16 = 1/2
For alphabet 0,1 and forbidden pattern 11, appending 0 returns either state to the empty suffix. Empty plus 1 enters suffix 1; suffix 1 plus 1 is blocked. Eight of sixteen length-four words survive.

The Bounded ordered allocations calculator counts integer tuples with a total constraint, while this page applies local substring restrictions to words.

Conditional prefix probabilities need a smaller denominator

If a fixed prefix has length r and the total length is n, only n−r characters remain free. There are k^(n−r) possible uniform continuations, not k^n. The main count includes only words beginning with that prefix and the reported probability is conditional on it. An empty prefix recovers the unconditioned count.

A prefix already containing a forbidden pattern makes every completion invalid. The count is then zero even if the final automaton suffix would otherwise look harmless. Likewise, when the prefix length equals n, there is exactly one possible completion: adding no characters. Its conditional probability is one or zero according to whether the fixed word is permitted.

A count is different from a word generator

The dynamic program keeps an exact count for each automaton state at each length. Different partial words reaching the same state are combined because they have the same allowed future transitions. No individual word list is needed, so lengths much larger than an enumerable sample space remain practical within the declared bounds.

The final count sums all nonforbidden states. Small examples can be verified by exhaustive word enumeration, including patterns contained inside longer patterns and patterns that overlap with themselves. Duplicate forbidden words are removed because forbidding the same substring twice adds no new restriction. A resource-state cap rejects an unsupported pattern family rather than returning a sampled or approximate count.

A few questions, answered

Can forbidden patterns overlap?

Yes. The suffix-prefix automaton keeps the information needed for overlapping matches; a match need not start after the previous one ends.

What does an empty word length mean?

With no fixed prefix and only nonempty forbidden patterns, there is one empty word and it is allowed. A nonempty prefix is invalid when the total length is zero.

Find your calculator

Start typing to explore the library.