Skip to main content
Back to problems
Leetcode
Medium
Strings
Arrays
Hash Maps
Rotate String

Determine whether one string can be rotated to become another string.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Given two strings s and goal, determine whether goal can be obtained by rotating s any number of positions.

A rotation moves some prefix of the string to the end while keeping the relative order of all characters the same. You may rotate the string zero or more times.

Return true if goal is reachable from s, otherwise return false.

Examples of a rotation

  • abcde -> cdeab
  • abcde -> deabc
  • Rotating by 0 positions leaves the string unchanged.

Input Format

  • Two strings s and goal.
  • Both strings consist of arbitrary characters, typically lowercase English letters in interview versions of the problem.

Output Format

  • Return a boolean value.
  • true if goal is a rotation of s, otherwise false.

Constraints

  • Compare lengths first: if s.length != goal.length, the answer is immediately false.
  • Strings may be empty.
  • Assume standard character equality comparisons.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "abcde", goal = "cdeab"

Output

true

Explanation

Rotating abcde left by 2 positions gives cdeab.

Example 2

Input

s = "abcde", goal = "abced"

Output

false

Explanation

No rotation of abcde can produce abced.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.