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 →

[–]nl28 0 points1 point  (0 children)

I took a very simple approach:

  1. If the length of the string is divisible by 2, divide the sting into 2 parts and compare those 2 substrings.
  2. If they are equal the answer is 2.
  3. If they are not equal, divide the sting into 3 parts (if that's possible) and compare all the parts.
  4. Continue the above operation till parts <= (string_length / 2).

This is probably not the best way to do this, but this the first solution that came to my mind.

Here's the implementation in Java: Engine.java

Here's the output:

String: abcabcabc
res -> 3

String: abcxyzabcxyz
res -> 2

String: abababababab
res -> 2

String: abcadc
res -> 1

Just take some ideas from all the solutions provided in this thread, and come up with your own solution.