Approach #1: Simulation [Accepted]

Intuition

We can simulate the position of the robot after each command.

Algorithm

Initially, the robot is at (x, y) = (0, 0). If the move is 'U', the robot goes to (x, y-1); if the move is 'R', the robot goes to (x, y) = (x+1, y), and so on.

Complexity Analysis

  • Time Complexity: , where is the length of moves. We iterate through the string.

  • Space Complexity: . In Java, our character array is .


Analysis written by: @awice.