Rotate String
Easy
Topics
A shift on s moves its leftmost character to the right-hand end, so "abcde" becomes "bcdea".
Return true if s can become t after some number of shifts, including none.
LeetCode names the second argument goal; the harness here calls it t.
Example 1
cdeab is a rotation of abcde gives true. Writing s twice puts every rotation of it somewhere in that doubled row, each as an unbroken block of 5 characters, so the question becomes whether t appears there. It starts at position 2, which is also how many shifts are needed.Input: s = "abcde", t = "cdeab" Output: true Explanation: Two shifts: "abcde" to "bcdea" to "cdeab".
Example 2
Input: s = "abcde", t = "abced" Output: false Explanation: The same letters, but no number of shifts produces that order.
Example 3
Input: s = "aa", t = "aa" Output: true Explanation: Zero shifts counts, and here every shift gives the same string anyway.
Constraints
- 1 <= s.length, t.length <= 100
- s and t consist of lowercase English letters
Editorial — intuition, approach, complexity and pitfalls
Intuition
Trying all n shifts and comparing each is perfectly fine at this size and it is worth writing once to see the shape of the problem. But there is a reframing that makes it a single search.
Write s twice: s + s. Every rotation of s appears in that doubled string as a contiguous block of n characters — the rotation by i starts at position i. So t is a rotation of s exactly when t is a substring of s + s.
The length check cannot be skipped. "a" is a substring of "abab" and is not a rotation of "ab", so the lengths have to match first. Forgetting that is the one way this approach goes wrong.
This is the standard trick for cyclic questions and it generalises: doubling turns "somewhere in the circle" into "somewhere in a line", after which ordinary substring search applies.
Approach
- If the lengths differ, return
false. - Build
s + s. - Return whether
toccurs in it as a substring. - The direct alternative: for each
ifrom0ton - 1, comparetagainsts[i:] + s[:i]and returntrueon a match.
Walkthrough
s = "abcde", t = "cdeab"
s + s = "abcdeabcde"
^^^^^
position 2 holds "cdeab"
and indeed every rotation sits in there:
position 0 abcde
position 1 bcdea
position 2 cdeab
position 3 deabc
position 4 eabcd
so the answer is true
s = "abcde", t = "abced"
"abced" does not occur in "abcdeabcde"
false
why the length check matters
s = "ab", t = "a"
s + s = "abab", and "a" IS a substring of it
but "a" is not a rotation of "ab"
comparing lengths first rejects it.Complexity
- Time — O(n^2) as written, O(n) with a linear substring search: the search scans the doubled string; a naive matcher may retry at each position, while a linear matcher does not.
- Space — O(n): the doubled string.
Common Pitfalls
- Omitting the length comparison, which returns
truefor any substring of the doubled string. - Doubling
tinstead ofs, which answers a different question when the lengths differ. - Comparing sorted characters or letter counts. That tests for an anagram, and
"abced"is an anagram of"abcde"without being a rotation. - Forgetting that zero shifts is allowed, so identical strings return
true. - Shifting in the wrong direction when writing the direct version. The leftmost character moves to the end.
Full Solution — all languages
class Solution:
def rotateString(self, s, t):
# Every rotation of s appears in s + s as a block of len(s)
# characters. The length check is not optional: "a" is a substring
# of "abab" but not a rotation of "ab".
return len(s) == len(t) and t in s + simport java.util.*;
class Solution {
public boolean rotateString(String s, String t) {
return s.length() == t.length() && (s + s).contains(t);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: bool rotateString(string s, string t) {
return s.size() == t.size() && (s + s).find(t) != string::npos;
}
};/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/ var rotateString = function(s, t) {
return s.length === t.length && (s + s).includes(t);
};