Java solution O(n) - Works with 'LLLL', 'RRRR', etc.


  • 0
    K
    class Solution {
        public boolean judgeCircle(String moves) {
            int x = 0, y = 0;
            
            for (int i = 0; i < moves.length(); i++) {
                if (moves.charAt(i) == 'U')
                    x++;
                
                if (moves.charAt(i) == 'D')
                    x--;
                
                if (moves.charAt(i) == 'L')
                    y = (y - 1) % 4;
                
                if (moves.charAt(i) == 'R')
                    y = (y + 1) % 4;
            }
            
            return (x == 0 && y == 0);
        }
    }

Log in to reply
 

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