Skip to main content

TM for equal numbers of x, y, z

Short answer

The TM makes passes, crossing out one x, one y, one z per pass by overwriting with X,Y,Z. After all passes, if no unmarked x,y,z remain, it accepts. For #xyxyzz#, two passes produce #XYXYZZ#; no unmarked symbols, so accepted.

Design a TM for L = {w ∈ {x,y,z}* : w has an equal number of x's, y's and z's}. Verify for #xyxyzz#.

L = { w ∈ {x,y,z}* : #x = #y = #z }. The delimiter # marks both ends of the input.

Idea. Make passes over the tape. In each pass, cross out one x (to X), then one y (to Y), then one z (to Z), returning to the left # after each. When no x remains, check that no y or z is left either; if the tape contains only X, Y, Z, accept.

StateReadWriteMoveNext
q0#sameRq1
q1y / z / X / Y / ZsameRq1
q1xXLr1
q1#sameLrC
r1x / y / z / X / Y / ZsameLr1
r1#sameRq2
q2x / z / X / Y / ZsameRq2
q2yYLr2
r2x / y / z / X / Y / ZsameLr2
r2#sameRq3
q3x / y / X / Y / ZsameRq3
q3zZLr3
r3x / y / z / X / Y / ZsameLr3
r3#sameRq1
rCx / y / z / X / Y / ZsameLrC
rC#sameRqD
qDX / Y / ZsameRqD
qD#sameSqa
  • q1: find an unmarked x. q2: find an unmarked y. q3: find an unmarked z. (r1, r2, r3 return to the left # after each.)
  • rC / qD: when no x is left, rewind and check that the whole tape holds only X, Y, Z.

If q2 or q3 cannot find a y or z (reaches #), there is no move, so the machine rejects.

Verification for #xyxyzz# (2 x's, 2 y's, 2 z's)

Pass 1:  mark x, y, z   -> #XYxyZz#
Pass 2:  mark x, y, z   -> #XYXYZZ#
q1 sweeps right and finds no x (reaches #): rewind with rC,
qD scans X Y X Y Z Z and meets # : accept

Selected configurations (⊢* means several moves): #[q1]xyxyzz# ⊢* #[q2]Xyxyzz# ⊢* #[q3]XYxyzz# ⊢* #[q1]XYxyZz# ⊢* #[q2]XYXyZz# ⊢* #[q3]XYXYZz# ⊢* #[q1]XYXYZZ# ⊢* #XYXYZZ[q1]# ⊢* #[qD]XYXYZZ# ⊢* #XYXYZZ[qD]# ⊢ #XYXYZZ[qa]#.

#xyxyzz# is accepted. A string like #xxyz# fails: after pass 1 gives #XxYZ#, pass 2 crosses out the second x, but no y remains, so q2 reaches # and halts without accepting.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Theory of Computation chapter 4 questions →