Simple JavaScript Solution [95ms]


  • 0
    H
    var judgeCircle = function(moves) {
        
       var  len = moves.length;
    
        var  uu,dd,ll,rr,ud;
            uu=dd=ll=rr=ud=0;
    
       // Calculate the number of steps taken in overall movement 
        for (i=0;i<len;i++)
            {
                if (moves.charAt(i) == "U")
                    {
                        uu+=1;                
                    }
                
                else if (moves.charAt(i) == "D")
                    {
                        dd+=1;   
                    }
                else if (moves.charAt(i) == "L")
                    {
                        ll+=1;
                    }
                
                else if (moves.charAt(i) == "R")
                    {
                        rr+=1;
                    }
                else
                    ud+=1;
            }
       // check whether number of steps in opposite directions are same or not.
        if(uu==dd & ll==rr)
            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.