Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0657 - Robot Return to Origin
    0657 - Robot Return to Origin
    0657 - Robot Return to Origin
    0657 - Robot Return to Origin

    0657 - Robot Return to Origin

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/robot-return-to-origin/

    Solution 1 - Count Moves

    Track the robot's position on a 2D plane starting at (0, 0). For each move, update the x or y coordinate. The robot returns to the origin if both coordinates are zero after all moves. Instead of tracking coordinates, we can simply count occurrences: return True if the number of 'U' moves equals 'D' and 'L' equals 'R'.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Coordinate Tracking

    Explicitly simulate the robot's movement by maintaining x and y coordinates. Increment or decrement based on each character in the moves string. Check if both coordinates return to zero.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0657.1 - Solution 1 - Count Moves """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def judgeCircle(self, moves: str) -> bool:
            """Robot Return to Origin Function"""
            return moves.count("U") == moves.count("D") and moves.count("L") == moves.count("R")
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().judgeCircle("UD"))        # True
        print(Solution().judgeCircle("LL"))        # False
        print(Solution().judgeCircle("RRDD"))      # False
        print(Solution().judgeCircle("LRUD"))      # True
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0657.2 - Solution 2 - Coordinate Tracking """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def judgeCircle(self, moves: str) -> bool:
            """Robot Return to Origin Function"""
            x, y = 0, 0
    
            for move in moves:
                if move == "U":
                    y += 1
                elif move == "D":
                    y -= 1
                elif move == "L":
                    x -= 1
                elif move == "R":
                    x += 1
    
            return x == 0 and y == 0
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().judgeCircle("UD"))        # True
        print(Solution().judgeCircle("LL"))        # False
        print(Solution().judgeCircle("RRDD"))      # False
        print(Solution().judgeCircle("LRUD"))      # True
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()