# Turing Machine for L = {a^n b^n | n>=1}

Prerequisite – Turing Machine

**Task :**

We have to design a Turing machine for a^{n}b^{n} where n>=1.

**Analysis :**

We can analyze that we have equal no of a’s and b’s and in some order i.e., first all a’s will come and then all b’s will come.

**Example :**

Input-1:aabbOutput-1:YESInput-2:aabbbbOutput-2:NOInput-3:ababOutput-3:NO

**Approach :**

Let us understand the approach by taking the example “aabb”.

- Scan the input from the left.
- First, replace an ‘a’ with ‘X’ and move right. Then skip all the a’s and b’s and move right.
- When the pointer reaches Blank(B) Blank will remain Blank(B) and the pointer turns left. Now it scans the input from the right and replaces the first ‘b’ with ‘Y’. Our Turing machine looks like this –

- Again the pointer reaches Blank(B) or X. It now scans the input from left to right. The pointer moves forward and replaces ‘a’ with ‘X’.
- Again the pointer reaches Blank(B) or Y. It now scans the input from the right to left. The pointer moves forward and replaces ‘b’ with ‘y’.
- We repeat the same steps until we convert all the a’s to ‘X’ and b’s to ‘Y’.
- When all the a’s converted to ‘X and all the b’s converted to ‘Y’ our machine will halt.

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.