Skip to content

Instantly share code, notes, and snippets.

@yokiy
Created June 24, 2014 00:33
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 yokiy/27b7f31ff00ffcc4f896 to your computer and use it in GitHub Desktop.
Save yokiy/27b7f31ff00ffcc4f896 to your computer and use it in GitHub Desktop.
CC 1.8
#cc 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").
def isRotation(s1, s2):
if (len(s1) == len(s2) and len(s1) > 0):
s = s1 + s1
return isSubstring(s, s2)
else:
return False
def isSubstring(s1, s2):
if (len(s1) > 0 and len(s1) > len(s2)):
for i in range(len(s1)):
k = i
j = 0
while s2[j] == s1[k]:
k += 1
j += 1
if (k == len(s1) or j == len(s2)):
return True
break
else:
return False
s1 = 'waterbottle'
s2 = 'terbott'
print 's1',s1, 's2', s2
print'Is s2 substring of s1:', isSubstring(s1, s2)
print 'Is s2 rotation of s1:', isRotation(s1, s2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment