Solution


  • 0
    A

    Python3 solutio:

    class Solution:
        def judgeCircle(self, moves):
            r=0
            d=0
            u=0
            l=0
            for i in moves:
                if(bool(i=='L')):
                    l=l+1
                elif(bool(i=='D')):
                    d=d+1
                elif(bool(i=='R')):
                    r=r+1
                elif(bool(i=='U')):
                    u=u+1
            print(l,r,u,d)
            if(bool(l==r) & bool(u==d)):
                return True
            else:
                return False        
    

  • 0
    S

    Java Solution, O(n) time

    class Solution {
        public boolean judgeCircle(String moves) {
            int x =0, y=0;
            for(int i=0 ; i< moves.length() ; i++)
            {
                switch(moves.charAt(i))
                {
                    case 'U' : x += 0 ; y += 1; break;
                    case 'D' : x += 0 ; y += -1; break;
                    case 'L' : x += -1 ; y += 0; break;
                    case 'R' : x += 1 ; y += 0; break;
                }            
            }
            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.