class Solution {
static class Point{
int x,y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public String solution(int[] numbers, String hand) {
HashMap<Integer, Point> pos = new HashMap<>();
String answer = "";
pos.put(1, new Point(0,0));
pos.put(2, new Point(0,1));
pos.put(3, new Point(0,2));
pos.put(4, new Point(1,0));
pos.put(5, new Point(1,1));
pos.put(6, new Point(1,2));
pos.put(7, new Point(2,0));
pos.put(8, new Point(2,1));
pos.put(9, new Point(2,2));
pos.put(0, new Point(3,1));
Point pLeft = new Point(3,0);
Point pRight = new Point(3,2);
for(int i=0; i<numbers.length; ++i){
int next = numbers[i];
if(next == 1 || next == 4 || next == 7){
pLeft = new Point(pos.get(next).x, pos.get(next).y);
answer += "L";
}
else if(next == 3 || next == 6 || next == 9){
pRight = new Point(pos.get(next).x, pos.get(next).y);
answer += "R";
}
else{
int ld = Math.abs(pos.get(next).x - pLeft.x)
+ Math.abs(pos.get(next).y - pLeft.y);
int rd = Math.abs(pos.get(next).x - pRight.x)
+ Math.abs(pos.get(next).y - pRight.y);
if(ld == rd){
if(hand.equals("left")){
pLeft = new Point(pos.get(next).x, pos.get(next).y);
answer += "L";
}
else{
pRight = new Point(pos.get(next).x, pos.get(next).y);
answer += "R";
}
}
else{
if(ld < rd){
pLeft = new Point(pos.get(next).x, pos.get(next).y);
answer += "L";
}
else{
pRight = new Point(pos.get(next).x, pos.get(next).y);
answer += "R";
}
}
}
}
return answer;
}
}