File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Python/chapter01/1.9 - String Rotation Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ String Rotation:Assume you have a method isSubstring which checks if one word is a
3
+ substring of another. Given two strings, sl and s2, write code to check if s2 is
4
+ a rotation of sl using only one call to isSubstring (e.g.,"waterbottle" is a
5
+ rotation of"erbottlewat").
6
+ """
7
+
8
+ def isSubstring (s1 ,s2 ):
9
+ if s2 in s1 : #check to see if the seqence of s2 is found in s1
10
+ return True
11
+ return False
12
+
13
+ def Rotation (s1 ,s2 ):
14
+ """
15
+ if they are substring of each other then we first check its lenght
16
+ """
17
+ if len (s1 ) != len (s2 ): #if there not the same lenght then there different woulds
18
+ return False
19
+
20
+ else :
21
+ s1 += s1 #waterbottlewaterbottle Anyroataion can be found here
22
+ return isSubstring (s1 ,s2 )
23
+
24
+ s1 = "waterbottle"
25
+ s2 = "erbottlewat"
26
+
27
+ print (Rotation (s1 ,s2 ))
You can’t perform that action at this time.
0 commit comments