Java Solution

  • 0

    Since this question is about tracking the location of an object, I decided to come up with a system that tracks the x value and y value of the robot.

    This solution consists of three parts:

    • creating variables for the x and y values as well as an array for direction values
    • assigning the "move" string's characters to the direction array
    • determining the end location of the robot

    code is below:

    class Solution {
        public boolean judgeCircle(String moves) {
            //create variables for the location and direction of the robot
            int x = 0;
            int y = 0;
            char[] direction = new char[moves.length()];
            //set the direction variables equal to each character in the string
            for (int i = 0; i < moves.length(); i++) {
                direction[i] = moves.charAt(i); 
                //add or subtract from location values to simulate "movement"
                if (direction[i] == 'U') {
                if (direction[i] == 'D') {
                if (direction[i] == 'L') {
                if (direction[i] == 'R') {
            //determine whether the ending location is at (0,0)
            if (x == 0 && y == 0) {
                return true;
            } else {
                return false;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.