Web Toolbar by Wibiya

Pages

Sunday, April 17, 2011

To check whether a string is a rotation of another string or not.

Solution #1: The first thing, of course, would be to make sure that both the strings have same length. Now, suppose these strings are str1 and str2 and one of them is a rotation of the other(eq. str1 = "google" and str2 = "oglego"). Append str2 to itself(making "oglegooglego"). Now, if we have a function to find a substring within a string, we can use it to find str1 in modified str2. If found, then str2 is a rotation of str1.

1 comment:

Cashonplex said...

here is a working code for this problem
http://www.nobrainer.co.cc/2012/01/determine-whether-string-is-rotation-of.html