I try to construct a TM that accepts the language $\{ ww \mid w \in \{a,b\}^* \}$.

Between the words $w$ is no delimeter, so I don't know, how my TM can know where the first $w$ ends and the second $w$ begins.

Is there a trick for this?

  • $\begingroup$ Hint: non-determinism. Or, you know, Turing machines can count. $\endgroup$
    – Raphael
    Jun 16, 2015 at 11:31
  • 3
    $\begingroup$ How would you try to find the spot where one halve ends and the other one begins in a very long string? $\endgroup$ Jun 16, 2015 at 12:52

3 Answers 3


Here is a sketch for how a deterministic machine might work:

  1. place a marker in front of the input.
  2. go through the input, checking if the length is even. If not, reject.
  3. place a marker behind the input.
  4. going back and forth, move the markers towards each other until they meet in the middle.
  5. as long as the halves are nonempty, compare and erase their first letters and reject if not equal.
  6. accept.
  • $\begingroup$ How can you check if the length is even? The Turing machine only knows what it is looking at right then. It can't count how many spaces it passes $\endgroup$ Jun 19, 2015 at 22:49
  • 1
    $\begingroup$ It only needs to keep track of the parity of the length - this can be done using two states between it keeps alternating. $\endgroup$ Jun 20, 2015 at 1:05

An important thing to recognize is that you are only concerned with deciding if your input $x$ can be written as $ww$. That is, you want if decide $\exists w (x=ww)$.

There are many ways you can go about this, and honestly, there is even a large variety of easy ways to go about it. If you just have to describe the Turing machine in words (like, you don't have to construct a detailed diagram), then pretty much any of them would be sufficient. If you have to draw a diagram, here are some things you might want to consider:

If you are allowed to use nondeterminism, as was commented, you should take advantage of this. You can nondeterministically guess where there half-way point of your input is, and then check to see if the first segment matches the second segment. Likewise, you can always just guess what $w$ is and then check to see that your input tape looks like $ww$.

If you can't use nondeterminism, you can always emulate it by trying all possible middle points or writing down all possible $w$'s, stopping at a provably reasonable time, of course. You can, as a previous answer mentions, count the length of your input $x$, then use that to compute the middle point.

Some other considerations that are typical when one is a assigned problems like these deals with how many tapes your machine has. If you Turing machine only has one tape, guessing $w$ might be foolish, and you would be much better off guessing or finding the middle.

I hope this is sufficient. The tricky thing about this kind of question is, either you don't have to fully describe the Turing machine, in which case, there are lots of nice and short answers, or you do have to fully describe the Turing machine, in which case, you will have to do a nice little bit of work to convert one of these short descriptions into a diagram.

  • $\begingroup$ I have to cunstruct a full TM. It isn't important if I use nendetermism or determinism, but i preffer the determinate TMs. So I construct a TM with 15 states all together in that way Klaus Draeger descripted it in the accepted answer. But thanks for your hints! $\endgroup$ Jun 16, 2015 at 15:55

One way I have read of is to first check the length of the string, and if it is odd, reject the string.

Then We could put some special character (e.g. c) at the end of the entire string and copy the entire string to the other side of the special character and then apply the algorithm for checking $a^nb^n$.

I also found another method explained as a pseudo code in this pdf, which changes the first half of the string to some other substitute variables and then compares them.

  • $\begingroup$ I don't understand, how you suggestion should work for my problem, but your link to the PDF was a great help! $\endgroup$ Jun 16, 2015 at 15:50
  • $\begingroup$ Care to explain why copying the string and running the $0^n1^n$ algorithm would help? why does it reject inputs of the form $xy$ with $|x|=|y|$ but $x\ne y$? $\endgroup$
    – Ran G.
    Jun 18, 2015 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.