-3

I need to solve the following problem:

Give a linear-time algorithm to determine if a text T is a cyclic rotation of another string T'. For example, arc and car are cyclic rotations of each other.

I have no idea where to start. How can I solve this problem?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • let xy to represent the text T, and yx to represent its rotated string and by concatenating them, by using KMP pattern initializing function, we can find the prefix length L, then compare the rest of the two substrings. However still in doubt – Bhudev Kumar Oct 14 '13 at 00:18
  • Here's the same question: https://stackoverflow.com/questions/2553522/interview-question-check-if-one-string-is-a-rotation-of-other-string. You can look for the accepted solution as well as the solution using KMP algorithm. P.S. I really liked the later one. – Adarsh Srivastava Nov 27 '20 at 05:22

1 Answers1

7

As a hint: if x and y have the same length, then x is a cyclic rotation of y iff x is a substring of yy. Try proving this and using this as the basis for your algorithm.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065