Rotate String
Real-world version: Carousel AlignmentThe 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.
def can_align_carousel(belt: str, target: str) -> bool:public boolean canAlignCarousel(String belt, String target)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find the Index of the First Occurrence in a StringReal-world version: Locate the Routing TagEasy
- Repeated Substring PatternReal-world version: Ribbon Repeats a MotifEasy
- Maximum Repeating SubstringReal-world version: Longest Run of a Repeated TagEasy
- Repeated String MatchReal-world version: Repeats Of A Motif Holding A StretchMedium