Detecting rotated string

Q. Assume you have a method isSubstring which checks if one word is a substring of another. Given two 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”)


def is_rotation(s1,s2):
    if s1 == "" and s2 == "":
        return True
    elif len(s1) != len(s2):
        return False
    else:
        if s2 in s1+s2:
            return True
        else:
            return False
        
        
if __name__ == "__main__":
    print is_rotation("waterbottle","erbottlewat")
    print is_rotation("Jyoti Sharma", "Sharma")

Advertisements

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