Designing a DFA for Binary Strings That Avoid Substring “101”
We want a deterministic finite automaton (DFA) over that accepts exactly the strings that do not contain the substring . This is a standard “pattern-avoidance” construction: the DFA tracks the longest suffix of the input so far that is also a prefix of the forbidden pattern (so we know how close we are to completing it).
Intuitively, we only need to remember the partial progress toward forming :
- we have matched nothing yet,
- we have matched a trailing
1, - we have matched a trailing
10, - if we ever complete
101, we must reject.
This approach is essentially the same idea as building a DFA using a prefix-function / failure-link style mechanism (as in KMP automata construction), but expressed directly as DFA states.
Below is a complete DFA design, its formal transition table, and a correctness argument.
DFA construction for avoiding substrings (pattern avoidance concept)
Key ideas and terminology
- substring
- prefix
- suffix
- accepting state
- trap (sink) state
Construct a DFA that avoids the forbidden substring 101
- 1Step 1
Let the forbidden substring be and . We will accept strings that never complete .
- 2Step 2
Create states representing the longest suffix of what you’ve read so far that matches a prefix of : for '', for '1', for '10'.
- 3Step 3
Add for having matched '101'. Once in , any further input keeps the string containing '101', so must be a trap.
- 4Step 4
For each state and input symbol, update the longest suffix that is a prefix of '101'. This can be done by reasoning about what suffix could end the newly extended string.
- 5Step 5
Accept exactly when you are not in . Hence accepting states are and the rejecting state is .
DFA Definition
States
Let the DFA be with:
- Start state:
- Accepting states:
- Rejecting/trap state:
Transition function
The transitions are determined by how the next input bit extends (or resets) the partial match.
| Current state | Read 0 → Next | Read 1 → Next | Meaning |
|---|---|---|---|
| No partial match; suffix '' | |||
| Suffix '1' matched | |||
Suffix '10' matched; next 1 completes '101' | |||
| Forbidden already found (trap) |
So, formally:
Correctness sketch (why this accepts exactly the safe strings)
We prove: the DFA accepts iff the input does not contain substring .
Invariant. After reading any input prefix , the automaton is in:
- if the longest suffix of that is a prefix of equals ''
- if that longest suffix equals '1'
- if that longest suffix equals '10'
- if already contains '101' (and then we stay in forever)
This invariant holds because transitions were defined by how the next symbol can extend or break the current partial match. In particular:
- From (meaning we currently have suffix '10'), reading
1forces completion of '101' → move to . - From , we loop on both symbols, so once rejected, always rejected.
Therefore:
- If the string ever contains '101', the DFA reaches at the moment that substring completes and rejects.
- If the string never contains '101', the DFA never reaches , so it ends in or and accepts.
State meanings vs. partial matches
Each state corresponds to the longest suffix that is also a prefix of '101'.
Common checks and edge cases
How the DFA progresses while scanning an input
Read nothing
StartIn (no relevant suffix)."
Track '1' as a partial match
After seeing 1In when the suffix is '1'."
Track '10' as a partial match
After seeing 10In when the suffix is '10'."
Forbidden pattern completes
When the next symbol is 1Move to upon reading '1' in state ; then reject forever."
DFA for avoiding 101 — quick checks
Knowledge Check
In the DFA that avoids the substring '101', what is the purpose of the trap state?