Skip to content

Instantly share code, notes, and snippets.

@habina
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habina/33b581c69f59fccfc1a6 to your computer and use it in GitHub Desktop.
Save habina/33b581c69f59fccfc1a6 to your computer and use it in GitHub Desktop.
"""
============================================================================
Question : 1.8 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").
Solution : Concatenate s1 with s1, check if s2 is a substring of s1 + s1
Time Complexity : Depend on isSubstring()
Space Complexity: O(n)
Gist Link : https://gist.github.com/habina/33b581c69f59fccfc1a6
============================================================================
"""
s1 = "waterbottle"
s2 = "erbottlewat"
s1_plus_s1 = s1 + s1
if(s2 in s1_plus_s1 and len(s1) == len(s2)):
print("s2 is a rotation of s1")
else:
print("s2 is not a rotation of s1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment