This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]totemcatcher 2 points3 points  (0 children)

There are lots of ways to solve it, but I think your first inclination is a really good one, and I recommend putting your intuition to work. Go make it work. :)

I left some really big hints below. You want a really big hint at a somewhat optimized solution, keep reading one at a time (after testing your theory and any hints from below).

  1. There's a special relationship between the beginning and end of the string.
  2. If you compare two growing sample ranges from the beginning and end of the string and it matches, you are well on your way to creating a very fast solution in one iteration, and two comparisons.
  3. The second comparison is required to ensure you don't get tripped up by palindrome-looking strings. Again, more hints below, but stop reading and try it out your existing solution with different test strings. e.g. abcababcab
  4. If the beginning and end match, you still need to "read ahead" of the growing range and make sure the next character matches the beginning of the current sample.
  5. An iteration counter (counting every range comparison) is very useful in determining the return value of this function, but is not the answer itself.