Show problems as
Classic problem #2833EasyStringCounting

Furthest Point From Origin

Real-world version: Smudged Cradle Log
Tracked in this browser only
Read Furthest Point From Origin on LeetCode(opens in a new tab)

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

A window-washing cradle begins the day parked at its rest position on the side of a tower. The shift log moves records, in order, what the cradle did during each stage of the day. One character stands for one stage:

  • 'U' means the cradle rose exactly one floor,
  • 'D' means the cradle dropped exactly one floor,
  • '?' means the entry is smudged, and that stage was either a rise of one floor or a drop of one floor.

The offset at the end of the day is how many floors separate the cradle's final position from its rest position, counted without regard to direction. Each smudged stage may be read either way, and two smudged stages need not be read the same way.

Return the largest offset that is consistent with the log.

Examples

Example 1

Input
moves = "UUDD??"
Output
2

Reading both smudged stages as drops leaves the cradle two floors below the rest position, so the offset is 2.

Example 2

Input
moves = "DDDU?"
Output
3

Reading the smudged stage as a drop leaves the cradle three floors below the rest position, so the offset is 3.

Example 3

Input
moves = "??????"
Output
6

Reading all six smudged stages as rises leaves the cradle six floors above the rest position, so the offset is 6.

Constraints

  • 1 <= moves.length <= 50
  • moves consists only of the characters 'U', 'D' and '?'.

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 furthest_offset(moves: str) -> int:
Java
public int furthestOffset(String moves)