← DiffPush

Check for rotated string

Baseline Strings · Easy O(N^2) · O(N)

Carousel Alignment Check

A museum display carousel steps leftward one item at a time, and curators photograph its arrangement at random moments. An archivist must confirm whether a photographed arrangement is just the reference lineup after some number of steps — the same items, same cyclic order, different starting point. Different lengths or a genuinely different cyclic order mean the photo shows something else entirely.

Input: Two strings s (the reference lineup) and goal (the photographed arrangement).

Output: true when goal can be reached from s by repeatedly taking its first character and appending it to the end, false otherwise.

Constraints

Examples

Example 1

Input: {"s":"abcde","goal":"cdeab"}
Output: true
Shifting a, b off the front and onto the back lands exactly on the photographed arrangement.

Example 2

Input: {"s":"abcde","goal":"abced"}
Output: false
Same length, same letters, but no rotation reproduces the swapped tail.

Solve this in your browser →

Also on LeetCode ↗