Constructing an NFA for “0+ then odd number of 1s then 2*” over Σ = {0,1,2}

Constructing an NFA for “0+ then odd number of 1s then 2*” over Σ = {0,1,2}

Verified Sources
Sep 12, 2026

We want an NFA over Σ={0,1,2}\Sigma=\{0,1,2\} that accepts exactly the strings of the form 0+(1(1) with odd number of 1s)2.0^+ \cdot (1(1)^* \text{ with odd number of }1\text{s}) \cdot 2^*. Concretely: the string must start with one or more 00s, then have an odd number of 11s, and may end with any number of 22s (including none).

A helpful regex-like characterization is: 0+1(1)2with the condition that the number of 1s is odd.0^+ \, 1\,(1\,)^* \, 2^* \quad \text{with the condition that the number of 1s is odd}. We’ll encode the “odd number of 1s” requirement by tracking parity using NFA states. This is a standard regular-language pattern, implemented either via DFA parity states or via NFA nondeterminism; here we’ll explicitly build the NFA.

Key ideas:

  • NFA: transitions can be nondeterministic.
  • Accepting state.
  • Start state.
  • Epsilon transition.

Language decomposition

Let’s view an accepted string as:

  1. Prefix: 0+0^+ (at least one 00)
  2. Middle: odd number of 11s (i.e., 11,13,15,1^1,1^3,1^5,\dots)
  3. Suffix: 22^* (any number of 22s)

We will ensure the NFA does not allow:

  • ending the string before the odd-number-of-11s portion is completed,
  • reading any symbol outside the required structure (e.g., no 00 after the middle begins).

Step-by-step NFA construction

  1. 1
    Step 1

    Use states for (i) being in the required 0+ prefix, (ii) tracking parity of the number of 1s read so far, and (iii) allowing only 2s after the odd-parity condition is satisfied.

  2. 2
    Step 2

    From the start, require at least one 0: after reading the first 0 you can remain in the 0-region on further 0s. On symbol 1, you may enter the 1-region only after at least one 0 was seen. Disallow 2 in this prefix.

  3. 3
    Step 3

    Track parity: after reading an even number of 1s you are not allowed to accept; after reading an odd number you are allowed to accept (or continue with 2s). Reading a 1 toggles parity (even <-> odd).

  4. 4
    Step 4

    Once you are in the 'odd number of 1s' state, allow reading any number of 2s while staying in an accepting configuration. Disallow any 0 after the middle begins.

  5. 5
    Step 5

    Accept exactly when the NFA can end in the 'odd parity' state after consuming the entire input (2* may have been read).

  6. 6
    Step 6

    Verify three cases: (i) strings with fewer than 1 zero are rejected, (ii) strings with even number of 1s are rejected, (iii) strings that have extra symbols in wrong positions are rejected.

Proposed NFA (explicit construction)

Let the NFA be M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) where:

  • Σ={0,1,2}\Sigma=\{0,1,2\}
  • Q={q0,q0,qE,qO}Q=\{q_0,q_{0},q_E,q_O\}
  • Start state: q0q_0
  • Accepting states: F={qO}F=\{q_O\}

Interpretation of states:

  • q0q_0: before reading the required first 00 (so empty or leading nonzero is impossible to accept)
  • q0q_{0}: have read at least one 00, still in the 0+0^+ prefix
  • qEq_E: have read an even number of 11s (and already transitioned out of the 0-prefix)
  • qOq_O: have read an odd number of 11s; from here you may read 22^* and acceptance depends on ending in qOq_O

Now define δ\delta as a set of transitions (for each state and symbol):

Prefix transitions (enforcing 0+0^+)

  • From q0q_0:
    • δ(q0,0)={q0}\delta(q_0,0)=\{q_{0}\}
    • δ(q0,1)=\delta(q_0,1)=\emptyset
    • δ(q0,2)=\delta(q_0,2)=\emptyset
  • From q0q_{0}:
    • δ(q0,0)={q0}\delta(q_{0},0)=\{q_{0}\} (keep consuming leading zeros)
    • δ(q0,1)={qE}\delta(q_{0},1)=\{q_E\} (on first 11, parity becomes even? careful: we start counting 1s here)
    • δ(q0,2)=\delta(q_{0},2)=\emptyset (no 2 allowed before 1s)

We must be consistent with parity:

  • If we enter the “1-region” on reading the first 11, then the number of 11s becomes 11 which is odd, not even. So we should set:
  • δ(q0,1)={qO}\delta(q_{0},1)=\{q_O\}

Parity transitions among 11s

  • From qEq_E:
    • δ(qE,1)={qO}\delta(q_E,1)=\{q_O\} (even -> odd after reading another 1)
    • δ(qE,0)=\delta(q_E,0)=\emptyset
    • δ(qE,2)=\delta(q_E,2)=\emptyset
  • From qOq_O:
    • δ(qO,1)={qE}\delta(q_O,1)=\{q_E\} (odd -> even)
    • δ(qO,0)=\delta(q_O,0)=\emptyset
    • δ(qO,2)={qO}\delta(q_O,2)=\{q_O\} (2* allowed after odd number of 1s)

Allow 2* suffix

  • Already captured by δ(qO,2)={qO}\delta(q_O,2)=\{q_O\}
  • Note: qEq_E has no outgoing transition on 22, so if you’ve read an even number of 11s, you cannot “skip” to trailing 22s.

Putting it all together, the transition function is:

Stateon 0on 1on 2
q0q_0{q0}\{q_{0}\}\emptyset\emptyset
q0q_{0}{q0}\{q_{0}\}{qO}\{q_O\}\emptyset
qEq_E\emptyset{qO}\{q_O\}\emptyset
qOq_O\emptyset{qE}\{q_E\}{qO}\{q_O\}

This is actually a DFA (no nondeterministic branching needed), but it is a valid NFA since DFA is a special case.

Why it matches the specification

  • Begins with one or more 0s: The only way to leave q0q_0 is by reading a 00, and acceptance is impossible unless you reach qOq_O later.
  • Odd number of 1s: Parity is tracked between qEq_E and qOq_O; each 1 toggles parity. qOq_O is accepting.
  • Ends with any number of 2s: From qOq_O, you can loop on 22 staying in qOq_O, so trailing 22s don’t change acceptance.
  • No extra 0s after entering the 1/2 phase: qEq_E and qOq_O have no transitions on 00.

type="tip" title="Pro Tip: parity states are the cleanest way to enforce “odd number of 1s”" content="Model the middle as a 2-state parity machine: even (qEq_E) vs odd (qOq_O). Then restrict symbol classes per phase (no 0 after starting ones; 2 only allowed after odd)."

type="warning" title="Common pitfall: allowing 2* too early breaks the “odd number of 1s” constraint" content="If you add transitions on 2 from the even-parity state (qEq_E), then inputs with even # of 1s followed by 2s would incorrectly be accepted. Our construction prevents this by having δ(qE,2)=\delta(q_E,2)=\emptyset."

Construction roadmap

Enforce 0+

Phase 1

Only leave the start on symbol 0; loop on 0 to guarantee at least one 0 exists."

Enter 1-region and track parity

Phase 2

The first 1 moves into odd-parity state; each subsequent 1 toggles parity."

Attach 2* after odd 1s

Phase 3

Allow 2-loop only from the odd-parity accepting state; disallow 2 from even-parity state."

Verify rejects

Phase 4

Test: missing zeros, even # of ones, and misplaced symbols (like 0 after ones)."

Quick checks and edge cases

Acceptance conditions by final state (conceptual)

If the input ends in q_O after consuming all symbols, the string is accepted; q_E or q_0 are rejecting.

Knowledge Check

Question 1 of 4
Q1Single choice

In the construction, which state(s) should be accepting to enforce an odd number of 1s?