Problem · String
Robot Final Direction
Learn this problemProblem statement
A robot starts at position 0 on a horizontal line and follows a string commands containing only L and R.
- Each
Lmoves the robot one step to the left. - Each
Rmoves the robot one step to the right.
After every command has been executed in order, return:
"L"if the robot stops to the left of its starting position.- An empty string if the robot stops at its starting position.
"R"if the robot stops to the right of its starting position.
A solution with time complexity no worse than O(commands.length^2) fits within the execution time limit.
Function
robotDirection(commands: String) → StringExamples
Example 1
commands = "RLLRLL"return = "L"The first two commands and the next two commands each return the robot to its starting position. The final two L commands leave it two steps to the left, so the result is "L".
Example 2
commands = "LLRLLLRRRR"return = ""The string contains five L commands and five R commands, so the robot returns to its starting position and the result is an empty string.
Example 3
commands = "RRL"return = "R"Two right moves and one left move leave the robot one step to the right, so the result is "R".
More Airbnb problems
- Most Frequent Reduced DigitOA · Seen Jul 2026
- Rectangle Fit QueriesOA · Seen Jul 2026
- Robot Inventory TrackingOA · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Print Sentences as TablePHONE SCREEN · Seen May 2026
- Minimum Eating SpeedSeen Mar 2024
- Minimum MovesSeen Dec 2022
- Donut ChallengeSeen Dec 2022