class Solution {
    public int leftNow = 10;
    public int rightNow = 12;
    public String answer = "";
    
    public String solution(int[] numbers, String hand) {
        for(int i=0; i<numbers.length; i++) {
            if(numbers[i]==0) { numbers[i] = 11; }
        }
        
        for(int i=0; i<numbers.length; i++) {
            if(numbers[i]==1 || numbers[i]==4 || numbers[i]==7) {
                answer += "L";
                leftNow = numbers[i];
            } else if(numbers[i]==3 || numbers[i]==6 || numbers[i]==9) {
                answer += "R";
                rightNow = numbers[i];
            } else if(numbers[i]==2 || numbers[i]==5 || numbers[i]==8 || numbers[i]==11) {
                if(checkHand(numbers[i], hand).equals("left")) {
                    answer += "L";
                    leftNow = numbers[i];
                } else {
                    answer += "R";
                    rightNow = numbers[i];
                }
            }
        }
        
        return answer;
    }
    
    public String checkHand(int number, String hand) {
        int leftRow = (leftNow-1)/3;
        int leftColumn = (leftNow-1)%3;
        int rightRow = (rightNow-1)/3;
        int rightColumn = (rightNow-1)%3;
        int numberRow = (number-1)/3;
        int numberColumn = (number-1)%3;

        int left = Math.abs(numberRow-leftRow)+Math.abs(numberColumn-leftColumn);
        int right = Math.abs(numberRow-rightRow)+Math.abs(numberColumn-rightColumn);
        
        return left<right ? "left" : left>right ? "right" : hand;
    }
}