-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1138.java
More file actions
30 lines (30 loc) · 841 Bytes
/
Copy pathSolution1138.java
File metadata and controls
30 lines (30 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution1138 {
public String alphabetBoardPath(String target) {
StringBuilder sb = new StringBuilder();
int x_last = 0, y_last = 0;
for (int i = 0; i < target.length(); i++) {
int dis = target.charAt(i) - 'a';
int x = dis % 5, y = dis / 5;
while (y < y_last) {
sb.append('U');
y_last--;
}
while (x < x_last) {
sb.append('L');
x_last--;
}
while (y > y_last) {
sb.append('D');
y_last++;
}
while (x > x_last) {
sb.append('R');
x_last++;
}
sb.append('!');
x_last = x;
y_last = y;
}
return sb.toString();
}
}