Fast python solution

  • 0

    The code here is a solution that beats 86% other solutions.

    class Solution(object):
        def judgeCircle(self, moves):
            :type moves: str
            :rtype: bool
            x = moves.count("R") - moves.count("L")
            y = moves.count("U") - moves.count("D")
            return x == 0 and y == 0

    Basically the information where the robot goes is not important at all. Important is to notice that if the robot goes Up, it has to go down. The solution is to simply sum Up and Down, Left and Right moves to see if those result in zeros.

  • 0

    If you want fast, you'd better not compute y when x already isn't 0.

Log in to reply

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