Prerequisite – Turing Machine

Design a Turing Machine for a string which contains exactly 3 repetitions of w consecutively.

**Approach Used :**

First, we will find the position of separation of the first w from the second w.

Now, we will match the first and second w. If both get, matched then the second w string will be converted to a string of $.

Then, we will match the first and third w. If both get, matched then the third w string will be converted to a string of $. If the string reaches the Halt state H, then it gets accepted.

**Example –**

Input: ababab Output: Accepted Input: abbabbabb Output: Accepted Input: ^ (Empty string) Output: Accepted Input: aba Output: Not accepted

**Step-1:**

If the symbol is $, replace it by $ and move right.

Go to state Q1 and step 2.

**Step-2:**

If the symbol is a, replace it by A and move right, or

If the symbol is b, replace it by B and move right.

Go to state Q2 and step 3.

————————————————-

If the symbol is A, replace it by A and move left, or

If the symbol is B, replace it by B and move left.

Go to state Q6 and step 7.

————————————————-

If the symbol is $, replace it by $, and the string is accepted.

Go to final state H.

**Step-3:**

If the symbol is a, replace it by a and move right, remain on the same state, or

If the symbol is b, replace it by b and move right, remain on the same state.

————————————————

If the symbol is A replace it by A and move left, or

If the symbol is B replace it by B and move left, or

If the symbol is $ replace it by $ and move left.

Go to state Q3 and step 4.

**Step-4:**

If the symbol is a replace it by A and move left, or

If the symbol is b replace it by B and move left,

Go to state Q4 and step 5

**Step-5:**

If the symbol is a replace it by A and move left, or

If the symbol is b replace it by B and move left,

Go to state Q5 and step 6

**Step-6:**

If the symbol is a, replace it by a and move left, remain on the same state, or

If the symbol is b, replace it by b and move left, remain on the same state.

————————————————

If the symbol is A, replace it by A and move right, or

If the symbol is B, replace it by B and move right, or

Go to state Q1 and step 2.

**Step-7:**

If the symbol is A, replace it by a and move left, remain on the same state, or

If the symbol is B, replace it by b and move left, remain on the same state

————————————————-

If the symbol if $, replace it by $ and move right.

Go to state Q7 and step 8.

**Step-8:**

If the symbol is a, replace it by A and move right.

Go to state Q9 and step 10.

————————————————–

If the symbol is b, replace it by B and move right.

Go to state Q8 and step 9.

————————————————–

If the symbol is $, replace it by $ and move left.

Go to state Q11 and step 12.

**Step-9:**

If the symbol is a, replace it by a and move right, remain on the same state, or

If the symbol is b, replace it by b and move right, remain on the same state, or

If the symbol is $, replace it by $ and move right, remain on the same state

————————————————–

If the symbol is B, replace it by $ and move left.

Go to state Q10 and step 11.

**Step-10:**

If the symbol is a, replace it by a and move right, remain on the same state, or

If the symbol is b, replace it by b and move right, remain on the same state, or

If the symbol is $, replace it by $ and move right, remain on the same state.

————————————————–

If the symbol is A, replace it by $ and move left.

Go to state Q10 and step 11.

**Step-11:**

If the symbol is a, replace it by a and move left, remain on the same state, or

If the symbol is b, replace it by b and move left, remain on the same state, or

If the symbol is $, replace it by $ and move left.

————————————————–

If the symbol is A, replace it by A and move right, or

If the symbol is B, replace it by B and move right.

Go to state Q7 and step 8.

**Step-12:**

If the symbol is A, replace it by a and move left, remain on the same state, or

If the symbol is B, replace it by b and move left, remain on the same state.

————————————————–

If the symbol is $, replace it by $ and move right.

Go to state Q12 and step 13.

**Step-13:**

If the symbol is a, replace it by A and move right.

Go to state Q14 and step 15.

————————————————–

If the symbol is b, replace it by B and move right.

Go to state Q13 and step 14.

————————————————–

If the symbol is $, replace it by $, and the string is accepted.

Go to final state H.

**Step-14:**

If the symbol is a, replace it by a and move right, remain on the same state, or

If the symbol is b, replace it by b and move right, remain on the same state, or

If the symbol is $, replace it by $ and move right, remain on the same state.

————————————————–

If the symbol is B, replace it by $ and move left.

Go to state Q15 and step 16.

**Step-15:**

If the symbol is a, replace it by a and move right, remain on the same state, or

If the symbol is b, replace it by b and move right, remain on the same state, or

If the symbol is $, replace it by $ and move right, remain on the same state.

————————————————–

If the symbol is A, replace it by $ and move left.

Go to state Q15 and step 16.

**Step-16:**

If the symbol is a, replace it by a and move left, remain on the same state, or

If the symbol is b, replace it by b and move left, remain on the same state, or

If the symbol is $, replace it by $ and move left.

————————————————–

If the symbol is A, replace it by A and move right, or

If the symbol is B, replace it by B and move right.

Go to state Q12 and step 13.

**Meaning of symbols- **

R- move right L- move left a- character a b- character b A- character A B- character B

**Example:**

We have to test the Turing machine for a string “ababab”.

-> $ababab$ -> $Ababab$ -> $AbabaB$ ->$AbabAB$ -> $ABabAB$ -> $ABaBAB$ -> $ABABAB$ -> $AbABAB$ -> $abABAB$ -> $AbABAB$ -> $Ab$BAB$ -> $AB$BAB$ -> $AB$$AB$ -> $Ab$$AB$ -> $ab$$AB$ -> $Ab$$AB$ -> $Ab$$$B$ -> $AB$$$B$ -> $AB$$$$$ (Accepted)

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- 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 language L = {0
^{n}1^{n}2^{n}| n≥1} - 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 = {0
^{2n}1^{n}| n>=0} - Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
- NPDA for the language L ={w∈ {a,b}*| w contains equal no. of a's and b's}
- NPDA for accepting the language L = {wwR | w ∈ (a,b)*}
- Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Turing Machine in TOC
- Turing Machine for subtraction | Set 2
- 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; k ≥ 1} - Turing Machine for addition
- Turing machine for multiplication
- Turing machine for subtraction | Set 1
- Turing machine for copying data
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i*j = k; i, j, k ≥ 1} - Turing machine for 1's and 2’s complement
- Turing Machine as Comparator

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.