# Construct a Turing Machine for language L = {0^{n}1^{n}2^{n} | n≥1}

Prerequisite – Turing Machine

The language L = {0^{n}1^{n}2^{n} | n≥1} represents a kind of language where we use only 3 character, i.e., 0, 1 and 2. In the beginning language has some number of 0’s followed by equal number of 1’s and then followed by equal number of 2’s. Any such string which falls in this category will be accepted by this language. The beginning and end of string is marked by $ sign.

**Examples –**

Input : 0 0 1 1 2 2 Output : Accepted Input : 0 0 0 1 1 1 2 2 2 2 Output : Not accepted

**Assumption:** We will replace 0 by X, 1 by Y and 2 by Z

**Approach used –**

First replace a 0 from front by X, then keep moving right till you find a 1 and replace this 1 by Y. Again, keep moving right till you find a 2, replace it by Z and move left. Now keep moving left till you find a X. When you find it, move a right, then follow the same procedure as above.

A condition comes when you find a X immediately followed by a Y. At this point we keep moving right and keep on checking that all 1’s and 2’s have been converted to Y and Z. If not then string is not accepted. If we reach $ then string is accepted.

**Step-1:**

Replace 0 by X and move right, Go to state Q1.**Step-2:**

Replace 0 by 0 and move right, Remain on same state

Replace Y by Y and move right, Remain on same state

Replace 1 by Y and move right, go to state Q2.**Step-3:**

Replace 1 by 1 and move right, Remain on same state

Replace Z by Z and move right, Remain on same state

Replace 2 by Z and move right, go to state Q3.**Step-4:**

Replace 1 by 1 and move left, Remain on same state

Replace 0 by 0 and move left, Remain on same state

Replace Z by Z and move left, Remain on same state

Replace Y by Y and move left, Remain on same state

Replace X by X and move right, go to state Q0.**Step-5:**

If symbol is Y replace it by Y and move right and Go to state Q4

Else go to step 1**Step-6:**

Replace Z by Z and move right, Remain on same state

Replace Y by Y and move right, Remain on same state

If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL STATE Q5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Construct a Turing Machine for a language L = {a
^{i}b^{j}c^{k}| i<j<k or i>j>k} ∩ {a^{i}b^{j}c^{k}| i>j>k or i>j>k} - Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
- Construct a Turing Machine for language L = {0
^{2n}1^{n}| n>=0} - Construct a Turing Machine for language L = {ww
^{r}| w ∈ {0, 1}} - Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i>j>k; k ≥ 1} - Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i< j< k; i ≥ 1} - Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i*j = k; i, j, k ≥ 1} - Construct Turing machine for L = {a
^{n}b^{m}a^{(n+m)}| n,m≥1} - Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i < j < k or i > j > k} - Turing Machine in TOC
- Turing Machine for subtraction | Set 2
- Variation of Turing Machine
- Turing machine for subtraction | Set 1
- Turing machine for multiplication
- Turing machine for 1's and 2’s complement
- Turing Machine as Comparator
- Turing Machine for addition
- Church’s Thesis for Turing Machine
- Modifications to standard Turing Machine

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.