Trains the technique from
LeetCode 777Swap Adjacent in LR StringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A single-file chute holds sliding tokens. A configuration of the chute is written as a string, one character per position: '-' is an empty position, 'L' is a token that can only travel toward the front of the chute (lower positions) and 'R' is a token that can only travel toward the back (higher positions).
One move is either of:
'L' whose immediately preceding position is empty, and slide it one position forward into that empty position;'R' whose immediately following position is empty, and slide it one position back into that empty position.A token can never pass through another token, and a token never changes kind.
Given the current configuration chute and a wanted configuration target, both of the same length, return true when some sequence of moves turns chute into target, and false otherwise. Zero moves is allowed.
Example 1
Sliding the `L` one position forward gives `L-R-`, then sliding the `R` one position back gives `L--R`. Both moves used an adjacent empty position in the token's own direction.
Example 2
The `R` sits at position 0 with the `L` immediately behind it, so it has no empty position to slide into, and the `L` has no empty position in front of it either. No move is possible at all, so `-RL` is out of reach.
Example 3
A move never removes a token, so a configuration with two `R` tokens can never become one with a single `R`.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def chute_reachable(chute: str, target: str) -> bool:public boolean chuteReachable(String chute, String target)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.