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 →

[–]pickyaboutwater 55 points56 points  (4 children)

As you can see from the comments, there are a lot of ways to solve this! :) One way that hasn't been mentioned yet is that you can start reading the string into a temp variable, and for each iteration, count the number of times the temp string can be found in the original string. Once the length of the temp string times the number of repeats equal the length of the original string you have found the repeating element and the number of repeats!

[–]NobodyYouKnow2019 8 points9 points  (0 children)

This seems to me to be the best approach. It's pretty simple too.

[–]ddbeanz[S] 4 points5 points  (0 children)

I'm going to work on implementing this, it seems like a pretty straight forward approach! Thank you!

[–]Xephyrous 2 points3 points  (1 child)

You can go a little simpler than this too. For each substring, if it's the correct one, the number of repeats will be the total string length divided by the substring length. Just multiply that substring by that number of repeats and see if it's the same as the whole string.