Show problems as
Classic problem #657EasyStringSimulation

Robot Return to Origin

Real-world version: Shuttle Back On The Dock
Tracked in this browser only
Read Robot Return to Origin on LeetCode(opens in a new tab)

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

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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