Check if One String is a Rotation of Other String

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

If s1 = “crazyforcode” then the following are some of its rotated versions:

“forcodecrazy”
“codecrazyfor”

Solution:

Steps:
First need to check if s1 and s2 are of the same length.
Then check, if s2 is a substring of s1 concatenated with s1.

boolean checkRotation(string s1, string s2) 
{
	if(s1.length() != s2.length())
		return false;
	if( substr(s2,concat(s1,s1))
		return true;
	return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation