Show problems as
Classic problem #796EasyStringString Matching

Rotate String

Real-world version: Carousel Alignment
Tracked in this browser only
Read Rotate String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A baggage carousel is a closed loop of slots, each stencilled with one lowercase letter. A scanner sits at one point of the loop and reads the letters clockwise from wherever the loop currently rests, producing the string belt.

Nudging the carousel forward by one slot moves the letter that was under the scanner to the far end of the reading and shifts everything else one place earlier. So a nudge turns "cargo" into "argoc".

Return true when some number of nudges, possibly zero, makes the scanner's reading equal target, and false otherwise.

Examples

Example 1

Input
belt = "loops", target = "opslo"
Output
true

Two nudges send l and o to the back of the reading, which lands exactly on the target.

Example 2

Input
belt = "loops", target = "loosp"
Output
false

The target uses the same five letters, but swapping the last two is not something nudging the loop can do.

Example 3

Input
belt = "loops", target = "ops"
Output
false

Nudging never removes a slot, so a shorter target can never be reached.

Constraints

  • 1 <= belt.length, target.length <= 100
  • belt and target contain lowercase English letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def can_align_carousel(belt: str, target: str) -> bool:
Java
public boolean canAlignCarousel(String belt, String target)