Java solution


  • 0
    S
    class Solution {
        public boolean judgeCircle(String moves) {
            int length = moves.length();
    		if ((length == 0) || (length % 2) !=0) return false;
    		int u=0,d=0,l=0,r = 0;
    		
    		for (int i=0; i < length; i++) {
    			switch (moves.charAt(i)) {
    			case 'U':
    				u = u + 1;
    				break;
    			case 'D':
    				d= d+1;
    				break;
    			case 'L':
    				l = l+1;
    				break;
    			case 'R':
    				r = r + 1;
    				break;
    			default:
    				break;
    			}	
    		}
    		if ((l -r) ==0  && (u -d) == 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.