Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e3ef41

Browse files
committedApr 4, 2025
Add solution #1138
1 parent 4fd6349 commit 3e3ef41

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,147 LeetCode solutions in JavaScript
1+
# 1,148 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -894,6 +894,7 @@
894894
1130|[Minimum Cost Tree From Leaf Values](./solutions/1130-minimum-cost-tree-from-leaf-values.js)|Medium|
895895
1131|[Maximum of Absolute Value Expression](./solutions/1131-maximum-of-absolute-value-expression.js)|Medium|
896896
1137|[N-th Tribonacci Number](./solutions/1137-n-th-tribonacci-number.js)|Easy|
897+
1138|[Alphabet Board Path](./solutions/1138-alphabet-board-path.js)|Medium|
897898
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
898899
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
899900
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|

‎solutions/1138-alphabet-board-path.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1138. Alphabet Board Path
3+
* https://leetcode.com/problems/alphabet-board-path/
4+
* Difficulty: Medium
5+
*
6+
* On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].
7+
*
8+
* Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"], as shown in the diagram below.
9+
*
10+
* We may make the following moves:
11+
* - 'U' moves our position up one row, if the position exists on the board;
12+
* - 'D' moves our position down one row, if the position exists on the board;
13+
* - 'L' moves our position left one column, if the position exists on the board;
14+
* - 'R' moves our position right one column, if the position exists on the board;
15+
* - '!' adds the character board[r][c] at our current position (r, c) to the answer.
16+
*
17+
* (Here, the only positions that exist on the board are positions with letters on them.)
18+
*
19+
* Return a sequence of moves that makes our answer equal to target in the minimum number of
20+
* moves. You may return any path that does so.
21+
*/
22+
23+
/**
24+
* @param {string} target
25+
* @return {string}
26+
*/
27+
var alphabetBoardPath = function(target) {
28+
let path = '';
29+
let row = 0;
30+
let col = 0;
31+
32+
for (const char of target) {
33+
const code = char.charCodeAt(0) - 97;
34+
const targetRow = Math.floor(code / 5);
35+
const targetCol = code % 5;
36+
37+
while (row > targetRow) path += 'U', row--;
38+
while (col > targetCol) path += 'L', col--;
39+
while (row < targetRow) path += 'D', row++;
40+
while (col < targetCol) path += 'R', col++;
41+
42+
path += '!';
43+
}
44+
45+
return path;
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.