Skip to content

Commit ef93c06

Browse files
committed
update string rotation comment
1 parent 318faf0 commit ef93c06

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Python/chapter01/1.9 - String Rotation/miguel_1.9_sol.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ def string_rotation(s1: str, s2: str) -> bool:
2525
"""
2626
Given two strings, string_rotation will check if s2 is a rotation of s1
2727
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.
3037
Runtime: O(n)
3138
Space Complexity: O(1)
3239
:param s1: the rotated string

0 commit comments

Comments
 (0)