Last active
August 29, 2015 14:03
-
-
Save habina/33b581c69f59fccfc1a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
============================================================================ | |
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