O(n) Java Solution

  • 0

    class Solution {
    public boolean judgeCircle(String moves) {
    if(moves.length()%2!=0) return false;
    int[] arr = new int[4];
    for(int i=0;i<moves.length();i++){
    if(moves.charAt(i)=='R') arr[0]++;
    else if(moves.charAt(i)=='L') arr[1]++;
    else if(moves.charAt(i)=='U') arr[2]++;
    else if(moves.charAt(i)=='D') arr[3]++;
    return arr[0]==arr[1]&&arr[2]==arr[3];

Log in to reply

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