All problems
0324EasyStringSimulation

Shuttle Back On The Dock

Tracked in this browser only
Write code

Trains the technique from

LeetCode 657Robot Return to Origin

This 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 floor shuttle in a parts warehouse begins parked on its charging dock and then plays back a tape of single-letter steps, moves, one step per letter, each step covering one tile:

  • 'U' moves it one tile up the aisle,
  • 'D' moves it one tile down the aisle,
  • 'L' moves it one tile to the left,
  • 'R' moves it one tile to the right.

The floor is unobstructed, so every step is carried out exactly as written and the shuttle may pass over the dock tile as often as it likes mid-tape.

Return true when the shuttle is sitting on the dock tile once the whole tape has been played back, and false when it stops anywhere else.

Examples

Example 1

Input
moves = "RRLL"
Output
true

Two steps right then two steps left leave the shuttle on the dock tile, so the answer is true.

Example 2

Input
moves = "RRUL"
Output
false

The shuttle finishes one tile to the right of the dock and one tile up the aisle from it, so it is not parked.

Example 3

Input
moves = "ULDR"
Output
true

The shuttle walks a small loop, passing three other tiles, and its last step puts it back on the dock.

Example 4

Input
moves = "D"
Output
false

A single step down the aisle leaves the shuttle one tile away from the dock.

Constraints

  • 1 <= moves.length <= 2 * 10^4
  • moves only contains the characters 'U', 'D', 'L' and 'R'.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def ends_on_dock(tape: str) -> bool:
Java
public boolean endsOnDock(String tape)
September 7
Apply