Skip to content

Commit b1d9899

Browse files
add 1138
1 parent 424e885 commit b1d9899

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ _If you like this project, please leave me a star._ ★
223223
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | |Easy||
224224
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk) |Easy||
225225
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Javascript](./javascript/_1146.js)| | Easy ||
226+
|1138|[Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1138.java)| | Medium |HashTable, String|
226227
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | |Easy||
227228
|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java) | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy||
228229
|1133|[Largest Unique Number](https://leetcode.com/problems/largest-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1133.java) | [:tv:](https://youtu.be/Fecpt1YZlCs)|Easy||
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1138 {
7+
public static class Solution1 {
8+
public String alphabetBoardPath(String target) {
9+
Map<Character, int[]> map = initMap();
10+
StringBuilder sb = new StringBuilder();
11+
int[] currPos = new int[2];
12+
for (char c : target.toCharArray()) {
13+
moveToDest(currPos, map.get(c), sb);
14+
}
15+
return sb.toString();
16+
}
17+
18+
private void moveToDest(int[] currPos, int[] dest, StringBuilder sb) {
19+
int currRow = currPos[0];
20+
int currCol = currPos[1];
21+
int destRow = dest[0];
22+
int destCol = dest[1];
23+
while (currRow != destRow || currCol != destCol) {
24+
if (currRow < destRow) {
25+
while (currRow < destRow) {
26+
if (currCol != 0 && currRow == 4) {
27+
break;
28+
}
29+
sb.append("D");
30+
currRow++;
31+
}
32+
currPos[0] = currRow;
33+
}
34+
if (currRow > destRow) {
35+
while (currRow > destRow) {
36+
sb.append("U");
37+
currRow--;
38+
}
39+
currPos[0] = currRow;
40+
}
41+
if (currCol < destCol) {
42+
while (currCol < destCol) {
43+
sb.append("R");
44+
currCol++;
45+
}
46+
currPos[1] = currCol;
47+
}
48+
if (currCol > destCol) {
49+
while (currCol > destCol) {
50+
sb.append("L");
51+
currCol--;
52+
}
53+
currPos[1] = currCol;
54+
}
55+
}
56+
sb.append("!");
57+
return;
58+
}
59+
60+
private Map<Character, int[]> initMap() {
61+
Map<Character, int[]> map = new HashMap<>();
62+
int row;
63+
int col;
64+
int number = 0;
65+
for (char c = 'a'; c <= 'z'; c++, number++) {
66+
row = number / 5;
67+
col = number % 5;
68+
map.put(c, new int[]{row, col});
69+
}
70+
return map;
71+
}
72+
}
73+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1138;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1138Test {
10+
private static _1138.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1138.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("DDR!UURRR!!DDD!", solution1.alphabetBoardPath("leet"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("DDDDD!UUUUURRR!DDDDLLLD!", solution1.alphabetBoardPath("zdz"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)