Example 1: Contains “aaa”
NFA
NFA accepting strings over {a,b} containing three consecutive a‘s:

- Q={q0,q1,q2,q3}
- Transitions:
- q0a,bq0 (stay until we start counting)
- q0aq1 (first a)
- q1aq2 (second a)
- q2aq3 (third a, accept)
- q3a,bq3 (stay accepting)
- F={q3}
DFA Equivalent
Via subset construction, the DFA has up to 24=16 states, but many are unreachable.
Example 2: Contains “ab” as Substring
NFA

Intuitively: stay in q0, transition to q1 on a, then to q2 on b, stay in q2.
DFA

- Q={q0,q1,q2}
- Transitions:
| State | a | b |
|---|
| q0 | q1 | q0 |
| q1 | q1 | q2 |
| q2 | q2 | q2 |
F={q2}
Example 3: Modulo Counter
DFA: Divisible by 4 (Binary)
Accepts binary strings divisible by 4:

- Q={q0,q1,q2,q3} (remainder mod 4)
- δ(qr,0)=q(2r)mod4
- δ(qr,1)=q(2r+1)mod4
- F={q0}
Intuition
Reading binary left-to-right:
- Append 0: multiply by 2
- Append 1: multiply by 2, add 1
Example 4: Exact Pattern
DFA: Exactly “ab”

- Q={q0,q1,q2,qd}
- Transitions:
| State | a | b |
|---|
| q0 | q1 | qd |
| q1 | qd | q2 |
| q2 | qd | qd |
| qd | qd | qd |
F={q2}
Dead state qd ensures no other strings are accepted.
Example 5: At Most Two a‘s
DFA
Accepts strings with at most two a‘s:
- Q={q0,q1,q2,q3}
- States track how many a‘s seen so far
- qi: seen exactly i a‘s
- q3: seen three or more a‘s (rejecting trap)
| State | a | b |
|---|
| q0 | q1 | q0 |
| q1 | q2 | q1 |
| q2 | q3 | q2 |
| q3 | q3 | q3 |
F={q0,q1,q2}
Example 6: Union via NFA-ε
Languages
L1={w:w ends in a}
L2={w:w ends in b}
NFA-ε for L1∪L2
Create new start with ε-transitions to both machines:

Accepting: both F1 and F2.
Summary
- NFAs are easier to construct for pattern detection
- DFAs are canonical (unique minimal DFA)
- Union uses epsilon transitions in NFAs
- Counting/modulo uses state to track remainder