Is string a rotation of another?

Assume you have a method isSubstring which checks if one word is a substring of another. Given tow strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g., “waterbottle” is a rotation of”erbottlewat” ).

C++:
01 bool isRotation(string a, string b)
02 {
03     if(a.length()!=b.length())
04         return false;
05     a+=a;
06     if(a.find(b)!=-1)
07         return true;
08     else
09         return false;
10 }
Advertisements
This entry was posted in String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s