Alphabet Board Path LeetCode solution in Java

1
class Solution {
2
public String alphabetBoardPath(String target) {
3
int x = 0, y = 0;
4
StringBuilder sb = new StringBuilder();
5
for (int i = 0; i < target.length(); i++) {
6
char ch = target.charAt(i);
7
int x1 = (ch - 'a') / 5;
8
int y1 = (ch - 'a') % 5;
9
while (x1 < x) {
10
x--;
11
sb.append('U');
12
}
13
while (y1 > y) {
14
y++;
15
sb.append('R');
16
}
17
while (y1 < y) {
18
y--;
19
sb.append('L');
20
}
21
while (x1 > x) {
22
x++;
23
sb.append('D');
24
}
25
sb.append('!');
26
}
27
return sb.toString();
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0