Robot Return to Origin

🏠 ⬅️ ➡️

There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).

Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.

Note: The way that the robot is "facing " is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: moves = "UD " Output: true Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

Example 2:

Input: moves = "LL " Output: false Explanation: The robot moves left twice. It ends up two "moves " to the left of the origin. We return false because it is not at the origin at the end of its moves.

Constraints:

  • 1 <= moves.length <= 2 * 104
  • moves only contains the characters 'U', 'D', 'L' and 'R'.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=3) :: moves

    moves = 'UD '
    print '(L1)', judgeCircle(moves)

    moves = 'LL '
    print '(L1)', judgeCircle(moves)

contains

logical function judgeCircle(moves) result(isAtOrigin)
    implicit none
    character(len=*), intent(in) :: moves
    integer :: i

    isAtOrigin = .true.
    do i = 1, len(moves)
        select case(moves(i:i))
            case('R')
                isAtOrigin = isAtOrigin .and. (0 == modulo(i-1, 2))
            case('L')
                isAtOrigin = isAtOrigin .and. (0 == modulo(i-1, 2))
            case('U')
                isAtOrigin = isAtOrigin .and. (0 == modulo(i-1, 2))
            case('D')
                isAtOrigin = isAtOrigin .and. (0 == modulo(i-1, 2))
            case default
                stop 'Invalid input.'
        end select
    end do
end function judgeCircle

end program main
Compiled
Executed
Correct
! Declare variables
integer :: x, y, i
character(len=100) :: moves

! Initialize variables
x = 0
y = 0

! Loop through each move
do i = 1, len(moves)
    select case(moves(i:i))
        case('U')
            y = y + 1
        case('D')
            y = y - 1
        case('L')
            x = x - 1
        case('R')
            x = x + 1
        case default
            write(*,*) "Invalid move"
            stop
    end select
end do

! Check if the robot is at the origin
if (x == 0 .and. y == 0) then
    write(*,*) "True"
else
    write(*,*) "False"
end if

end
🌐 Data from online sources
def judgeCircle(moves: str) -> bool:
    x, y = 0, 0
    for move in moves:
        if move == 'U': y += 1
        elif move == 'D': y -= 1
        elif move == 'R': x += 1
        elif move == 'L': x -= 1
    return x == 0 and y == 0

The algorithm keeps track of the robot's position in a 2D plane using two integer variables x and y initialized to 0. The x represents the horizontal position, and y represents the vertical position. It then iterates through the given moves string and updates the position based on the current move. If a move is 'R', it increments x; if it's 'L', it decrements x; if it's 'U', it increments y; and if it's 'D', it decrements y. After processing all the moves, the algorithm checks if both x equals to 0 and y equals to 0. If both are 0, it means the robot is back to the origin, and it returns true; otherwise, it returns false.

🌐 Data from online sources
bool judgeCircle(string moves) {
    int x = 0, y = 0;
    for(char move : moves) {
        if(move == 'U') y++;
        else if(move == 'D') y--;
        else if(move == 'R') x++;
        else if(move == 'L') x--;
    }
    return x == 0 && y == 0;
}

The algorithm keeps track of the robot's position in a 2D plane using two integer variables x and y initialized to 0. The x represents the horizontal position, and y represents the vertical position. It then iterates through the given moves string and updates the position based on the current move. If a move is 'R', it increments x; if it's 'L', it decrements x; if it's 'U', it increments y; and if it's 'D', it decrements y. After processing all the moves, the algorithm checks if both x equals to 0 and y equals to 0. If both are 0, it means the robot is back to the origin, and it returns true; otherwise, it returns false.