Robot Final Direction
Problem statement
A robot starts at position 0 on a number line. Execute the characters of commands in order: L moves the robot one position left, and R moves it one position right.
Return "L" if the final position is left of the starting point, "R" if it is right of the starting point, or the empty string "" if the robot returns to its starting point.
For this exercise, assume the command string may be empty; an empty sequence leaves the robot at its starting point.
Function
finalRobotDirection(commands: String) → StringExamples
Example 1
commands = "LRR"return = "R"The positions are -1, 0, and 1. The robot finishes to the right.
Example 2
commands = "RLL"return = "L"The positions are 1, 0, and -1. The robot finishes to the left.
Example 3
commands = "LRRL"return = ""There are two left moves and two right moves, so the final position is 0.
Constraints
- For this exercise, assume
0 <= commands.length <= 10^5. - Every character of
commandsisLorR.