DFA Design: Strings over {a,b}\{a,b\} with at least three occurrences of the substring bbbbbb (overlap allowed)

DFA Design: Strings over {a,b}\{a,b\} with at least three occurrences of the substring bbbbbb (overlap allowed)

Verified Sources
Sep 14, 2026

We want a deterministic finite automaton (DFA) over the alphabet {a,b}\{a,b\} that accepts exactly the strings containing at least three occurrences of the substring bbbbbb, where occurrences may overlap. For example, bbbbbbbbbb contains occurrences at positions 1 ⁣ ⁣31\!-\!3, 2 ⁣ ⁣42\!-\!4, and 3 ⁣ ⁣53\!-\!5, so it must be accepted.

A robust way to build such a DFA is to track:

  1. How much of the current suffix could still be part of a future bbbbbb (i.e., the last 0,1,20,1,2 symbols of a potential bbbbbb ending),
  2. How many bbbbbb occurrences have already been completed,
  3. And to increment the count whenever the transition produces a completed bbbbbb.

We will use the standard “prefix-suffix” memory for the pattern bbbbbb, i.e., track the longest suffix of the processed input that is also a prefix of bbbbbb.

Key concepts:

  • Deterministic finite automaton (DFA)
  • Substring occurrence
  • Overlapping occurrences
  • Prefix of a pattern

Mermaid view of the idea:

DFA Design for Substring Matching (automaton intuition)

Formal target language

Let Σ={a,b}\Sigma=\{a,b\}. Define the set of strings

L={wΣw contains at least 3 (possibly overlapping) occurrences of bbb}.L=\{\,w\in\Sigma^* \mid w \text{ contains at least 3 (possibly overlapping) occurrences of } bbb\,\}.

We can formalize an “occurrence” as: bbbbbb occurs starting at position ii if the length-3 factor w[i]w[i+1]w[i+2]=bbbw[i]w[i+1]w[i+2]=bbb.

To handle overlap, we must ensure that after reading a symbol that completes one occurrence, the automaton continues tracking suffixes that may begin another occurrence immediately.

State design strategy (suffix memory + count)

The pattern is bbbbbb. Its prefixes are:

  • length 00: ϵ\epsilon
  • length 11: bb
  • length 22: bbbb
  • length 33: bbbbbb (a completion)

Let the DFA state be a pair:

  • count
  • suffix

We only need suffix lengths 0,1,20,1,2 explicitly; suffix length 33 is handled by “increment count then fall back to the appropriate suffix” (because overlaps are allowed).

We cap the count at 33: once we’ve seen 33 occurrences, we stay in an accepting “done” region.

Suffix transitions for the pattern bbbbbb when reading symbols

Let suffix memory m{0,1,2}m\in\{0,1,2\} mean:

  • m=0m=0: suffix is “nothing useful” (no trailing bb prefix)
  • m=1m=1: suffix is bb
  • m=2m=2: suffix is bbbb

Now compute updates on reading next symbol:

  • Reading aa always breaks any run of bb:

    • from any mm: suffix becomes 00 (because suffix cannot end in bb).
  • Reading bb extends a run:

    • from m=0m=0 (“no trailing b-prefix”) reading bb gives suffix 11 (“ends with bb”)
    • from m=1m=1 reading bb gives suffix 22 (“ends with bbbb”)
    • from m=2m=2 reading bb completes one bbbbbb occurrence:
      • count increments
      • after consuming that bbbbbb, the new suffix that can start another bbbbbb due to overlap is the suffix bbbb (because the last two symbols are bbbb)

That last step is the overlap mechanism: in a run of bb’s, consecutive bbbbbb windows share characters.

[CalloutBlock] type="tip" title="Pro Tip" content="When overlaps are allowed, don’t reset to 0 after finding a match. Instead, compute the longest suffix that is also a prefix of the pattern (here, after matching bbbbbb the remaining suffix is bbbb)."

Construct the DFA (complete transition table)

  1. 1
    Step 1

    Use states (c,m)(c,m) where c{0,1,2,3}c\in\{0,1,2,3\} counts completed bbbbbb occurrences (cap at 3) and m{0,1,2}m\in\{0,1,2\} is suffix memory for possible bb-prefix length. Total states: 4×3=124\times 3=12.

  2. 2
    Step 2

    Start at (0,0)(0,0) (no occurrences yet; no relevant trailing bb).

  3. 3
    Step 3

    Accept all states with c=3c=3, i.e., (3,0),(3,1),(3,2)(3,0),(3,1),(3,2).

  4. 4
    Step 4

    For any cc and any mm, reading aa sends the DFA to (c,0)(c,0) because aa breaks any trailing bb run.

  5. 5
    Step 5

    If m=0m=0 then reading bb sends (c,0)(c,1)(c,0)\to(c,1). If m=1m=1 then (c,1)(c,2)(c,1)\to(c,2).

  6. 6
    Step 6

    If m=2m=2 then reading bb completes one bbbbbb: (c,2)(c+1,2)(c,2)\to(c+1,2) where c+1c+1 is capped at 3, i.e., (2,2)(3,2)(2,2)\to(3,2) and (3,2)(3,2)(3,2)\to(3,2).

Complete DFA specification

State set

Let

Q={(c,m)c{0,1,2,3}, m{0,1,2}}.Q=\{(c,m)\mid c\in\{0,1,2,3\},\ m\in\{0,1,2\}\}.

Start state:

q0=(0,0).q_0=(0,0).

Accepting states:

F={(3,0),(3,1),(3,2)}.F=\{(3,0),(3,1),(3,2)\}.

Transition function δ\delta

For any c{0,1,2,3}c\in\{0,1,2,3\}:

  1. On input aa:
δ((c,m),a)=(c,0)for m{0,1,2}.\delta((c,m),a)=(c,0)\quad \text{for } m\in\{0,1,2\}.
  1. On input bb:
  • If m=0m=0:
δ((c,0),b)=(c,1)\delta((c,0),b)=(c,1)
  • If m=1m=1:
δ((c,1),b)=(c,2)\delta((c,1),b)=(c,2)
  • If m=2m=2 (completion):
δ((c,2),b)=(min(c+1,3),2)\delta((c,2),b)=(\min(c+1,3),2)

This DFA is deterministic because for each state and each symbol a,ba,b, exactly one next state is defined.

[CalloutBlock] type="warning" title="Warning: counting overlaps correctly" content="The only time the count increases is when m=2m=2 and the next symbol is bb. This correctly counts overlapping bbbbbb windows (e.g., bbbbbbbbbb adds three matches)."

Transition table (compact)

Let cc' denote min(c+1,3)\min(c+1,3).

Current stateon aon b
(c,0)(c,0)(c,0)(c,0)(c,1)(c,1)
(c,1)(c,1)(c,0)(c,0)(c,2)(c,2)
(c,2)(c,2)(c,0)(c,0)(c,2)(c',2)

where c{0,1,2,3}c\in\{0,1,2,3\} and c=min(c+1,3)c'=\min(c+1,3).

Example runs (to validate overlap)

Example: bbbbbbbbbb

Track (c,m)(c,m):

  • Start (0,0)(0,0)
  • read bb: (0,0)(0,1)(0,0)\to(0,1)
  • read bb: (0,1)(0,2)(0,1)\to(0,2)
  • read bb: (0,2)(1,2)(0,2)\to(1,2) (first bbbbbb completed)
  • read bb: (1,2)(2,2)(1,2)\to(2,2) (second completed, overlapping)
  • read bb: (2,2)(3,2)(2,2)\to(3,2) (third completed)

Since c=3c=3, the string is accepted.

Example: bbbabbbbbbabb b idea

Any time an aa appears, mm resets to 0, which prevents bbbbbb from completing across the aa.

State diagram (pattern counter “layered by count”)

Below is a structural view: from each count layer, reading bb advances suffix memory; hitting m=2m=2 and reading bb moves to the next count layer while staying at m=2m=2.

How count increases in a long run of b’s

In a string of nn b’s, the number of overlapping bbbbbb occurrences is n2n-2 (for n3n\ge 3). The DFA acceptance threshold is at least 3 occurrences.

FAQ and edge cases

DFA Design Flashcards: bbb occurrence counter

1 / 4
Question · Term

What does suffix memory $m=2$ mean?

Click to reveal
Answer · Definition

The processed string ends with bbbb, i.e., we have the longest suffix that matches the length-2 prefix of bbbbbb.

Knowledge Check

Question 1 of 3
Q1Single choice

In the constructed DFA, in which situation does the automaton increase the completed-occurrence count?