FastPrepClassify Final Robot Displacement

Classify Final Robot Displacement

ZipRecruiter logoZipRecruiter● EasyFULLTIMEOA
Learn

Problem statement

A robot starts at vertical position 0 and follows the characters in commands from left to right.

  • U moves the robot one step up.
  • D moves the robot one step down.

Return \"U\" if the final position is above the start, an empty string if it is at the start, or \"D\" if it is below the start.

Function

classifyVerticalDisplacement(commands: String) → String

Examples

Example 1

commands = "UUDDUDU"return = "U"

Four up moves and three down moves leave the robot one step above the start.

Example 2

commands = "UDDU"return = ""

Two up moves and two down moves cancel, so the robot returns to its starting position.

Constraints

  • 1 <= commands.length <= 100000
  • commands contains only U and D.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String classifyVerticalDisplacement(String commands) {
    // Write your code here.
}
commands"UUDDUDU"
expected"U"
Checking account…