Interview question: Check if one string is a rotation of other string.

Posted by Webdev on Stack Overflow See other posts from Stack Overflow or by Webdev
Published on 2010-03-31T13:58:25Z Indexed on 2010/03/31 14:03 UTC
Read the original article Hit count: 282

Filed under:
|
|

A friend of mine was asked the following question today at interview for the position of software developer.

Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ?

Example: if s1 = "stackoverflow"; then the following are some of its rotated versions:

"tackoverflows" "ackoverflowst" "overflowstack"

where as "stackoverflwo" is not a rotated version.

The answer he gave was: Take s2 and find the logest prefix that is a substring of s1, that will give you the point of rotation. Once you find that point, break s2 at that point to get s2a and s2b, then just check if concatenate(s2a,s2b) == s1

It looks like a good solution to me and my friend. But the interviewr though otherwise. He asked for a simpler solution. Please help me by telling how would you do this in Java/C/C++ ?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about interview-questions

Related posts about java