LeetCode: Rotate String
Problem:
https://leetcode.com/problems/rotate-string/description/
Code:
# OPTIMISED
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s)==len(goal) and goal in (s+s)
# BELOW TWO SOLUTIONS NOT (O(N^2))
# class Solution:
# def rotateString(self, s: str, goal: str) -> bool:
# sq=deque(s)
# goal_q=deque(goal)
# i=0
# while i<len(sq):
# i+=1
# sq.append(sq.popleft())
# if sq==goal_q:
# return True
# return False
# ======= These both are n^2 =========
# class Solution:
# def rotateString(self, s: str, goal: str) -> bool:
# sq=deque(s)
# i=0
# while i<len(sq):
# i+=1
# sq.append(sq.popleft())
# if ''.join(sq) == goal:
# return True
# return False
Key Points:
Problem in Simple Words
You have two strings: s and goal.
You can rotate s by moving the first character to the end any number of times.
You must check: Can we turn s into goal using such rotations?
Example:
s = "abcde"→ rotations:"abcde","bcdea","cdeab","deabc","eabcd"If
goalis one of these, answer isTrue.
Core Idea (Smart Trick with s + s)
Key observation:
If
goalis a rotation ofs, thengoalwill always appear as a substring ofs + s.
Example:
s = "abcde"s + s = "abcdeabcde"All rotations of
"abcde"are inside this:
"abcde","bcdea","cdeab","deabc","eabcd"
So we just need to:
Check that
sandgoalhave the same length
(rotating doesn’t change length)Check if
goalis a substring ofs + s
If both are true → goal is a rotation of s.
Why Length Check Is Important
- If lengths differ, rotation is impossible.
No need to do the substring check at all.
Why This Is Better Than Simulating Rotations
A slower way is:
Actually rotate
sone step at a timeCompare with
goalafter each rotation
That takes:
Up to
nrotationsEach rotation + comparison can cost
O(n)Total ≈
O(n²)
The s + s trick:
Builds
s + sonceUses substring search which is
O(n)(optimized internally)Total ≈
O(n)
Complexity
Time:
O(n)Space:
O(n)fors + s
Simple, clean, and very efficient.