[Java][Easy to Understand]


  • 0

    The idea is that the number of left moves and right moves must be same.

    class Solution {
        public boolean judgeCircle(String moves) {
            
            char[] moveArray = moves.toCharArray();
            int leftCount = 0 ;
            int rightCount = 0;
            int upCount = 0;
            int downCount = 0;
            
            for(char ch : moveArray) {
                if(ch == 'L')
                    leftCount++;
                if(ch == 'R')
                    rightCount++;
                if(ch == 'U')
                    upCount++;
                if(ch == 'D') 
                    downCount++;
            }
            
            if(leftCount == rightCount && upCount == downCount)
                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.