vamsu
8/4/2018 - 8:55 PM

Is move ciricular

Is move circular

import java.io.*;
import java.util.*;

class Solution {
  public static void main(String[] args) {
    Solution solution =new Solution();
    String[] input = {
          null,
          "",
          "MRMRMRM",
          "MLMLMLM",
          "MRMLMRMRMMRMM",
          "MMMRRRLLLMMRLMRL",
          ""
        };
    for(int i=0;i< input.length;i++) {
      System.out.println("Input: " + input[i] + 
                       ", Result: " + solution.circularMoves(input[i]));
    }
  }
   
  public boolean circularMoves(String moves){
    if(moves==null || moves.length() < 1) {
      return false;
    }
    int direction = 0;
    //0-N, 1- E, 2-S, 3-W
    int x = 0;
    int y = 0;
    
    for(char move : moves.toCharArray()) {
      if(move == 'M') {
        if(direction == 0) {
          y++;
        } else if(direction == 1) {
          x++;
        } else if(direction == 2) {
          y--;
        } else if(direction == 3){  
          x--;
        }
      } else if (move == 'L') {
        direction = (direction - 1 + 4) % 4;
      } else if (move == 'R') {
        direction = (direction +1)%4;
      }
    }
    return x==0 && y==0;
  }  
  
}