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

• Difficulty Level : Medium
• Last Updated : 11 Nov, 2020

Prerequisite – Turing Machine

We have to design a Turing machine for anbn 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:aabb
Output-1:YES

Input-2:aabbbb
Output-2:NO

Input-3:abab
Output-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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :