File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
Python/chapter01/1.9 - String Rotation Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -25,8 +25,15 @@ def string_rotation(s1: str, s2: str) -> bool:
25
25
"""
26
26
Given two strings, string_rotation will check if s2 is a rotation of s1
27
27
using only one call to isSubstring.
28
- If s2 is a rotation of s1, then when we add s1 to itself, s2
29
- must be a substring of s1 + s1
28
+ Assume s2 is a rotation of s1. s1 is a rearrangement of s2.
29
+ Choosing an index where we rotate to be any index,
30
+ let s1 and s2 consist of two parts, a and b.
31
+ The original string will be s2 = ab.
32
+ The rotated string will be s1 = ba.
33
+ Since we are using substring, we want s2 = ab to appear in
34
+ a larger string ..ab..
35
+ This is guaranteed to happen if we have s1 + s1 = baba
36
+ because ab shows up in there.
30
37
Runtime: O(n)
31
38
Space Complexity: O(1)
32
39
:param s1: the rotated string
You can’t perform that action at this time.
0 commit comments