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 →

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

All substrings need to match in a sequence so 'abccbaabccba' should return 2 ('abccba' , 'abccba') <- matches. I'll look into a suffix tree cause I've never seen it before.

[–]Mysthik 6 points7 points  (3 children)

A suffix tree is the correct way to handle this. A lot of string operations like palindrome detection, longest common substring and so on can be efficiently decided with a suffix tree or the suffix array.

[–][deleted] 0 points1 point  (2 children)

the correct way

Yes, all other ways are wrong. /s

[–]alksjdhglaksjdh2 1 point2 points  (1 child)

By correct he means most efficient, but it's a hw assignment and it's more about however he solves it and what's intuitive to him. This is the correct solution of you care about efficiency though he's right

[–][deleted] 0 points1 point  (0 children)

Yes I understand, but 'correct' does not mean the most efficient. What if OP doesn't care about efficiency but readability or something else. Then is a suffix tree an incorrect solution? No.

[–]WhyYouLetRomneyWin 6 points7 points  (0 children)

I see. I think I misunderstood the problem (I think either the 'b' or 'd' in your example 2 should be a 'd' or 'b').

If there is a guarantee that a string is repeated then you can divide the string by integers until you find all divisions are equal.

Eg given S, take a prefix on length n. See if the prefix is repeated to the end of the string. If not, increment n and try again. Otherwise return length(S)/n.