Rotate String

Easy

Topics
StringString Matching

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

s + sa0b1c2d3e4a5b6c7d8e9
tcdeab
where t sitst
Asking whether 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.
A shift moves the leftmost character to the right-hand end; zero shifts is allowed.
Input:  s = "abcde", t = "cdeab"
Output: true
Explanation: Two shifts: "abcde" to "bcdea" to "cdeab".

Example 2

s + sa0b1c2d3e4a5b6c7d8e9
tabced
t
The two use exactly the same letters and it is still false, which is the distinction worth keeping: an anagram is not a rotation.
Input:  s = "abcde", t = "abced"
Output: false
Explanation: The same letters, but no number of shifts produces that order.

Example 3

s + sa0a1a2a3
taa
where t sitst
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

  1. If the lengths differ, return false.
  2. Build s + s.
  3. Return whether t occurs in it as a substring.
  4. The direct alternative: for each i from 0 to n - 1, compare t against s[i:] + s[:i] and return true on 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 true for any substring of the doubled string.
  • Doubling t instead of s, 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 + s
SensAI
Beta
Tap the mic to stop voice mode