-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1138-alphabet-board-path.js
More file actions
40 lines (37 loc) · 1.12 KB
/
1138-alphabet-board-path.js
File metadata and controls
40 lines (37 loc) · 1.12 KB
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
31
32
33
34
35
36
37
38
39
40
// map letter to board position
const board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
const h = {}
for(let i=0; i<board.length; i++) {
for(let j=0; j<board[i].length; j++) {
const ltr = board[i][j]
h[ltr] = [i,j]
}
}
const getPath = (start, end, isEndZ) => {
// edge case: need to consider if start or end letter is z
const vertDist = start[0]-end[0]
const vertMove = (vertDist>0) ? "U" : "D"
const horizDist = start[1]-end[1]
const horizMove = (horizDist>0) ? "L" : "R"
const horiz = Array(Math.abs(horizDist)).fill(horizMove).reduce((acc, m) => acc+m, "")
const vert = Array(Math.abs(vertDist)).fill(vertMove).reduce((acc, m) => acc+m, "")
return isEndZ ? horiz+vert+"!" : vert+horiz+"!"
}
/**
* @param {string} target
* @return {string}
*/
var alphabetBoardPath = function(target) {
if(target.length === 0) return ""
// approach: graph search to find the next letter. Then calculate path
let start = [0,0]
let end
let res = ""
for(let i=0; i<target.length; i++) {
const ltr = target[i]
end = h[ltr]
res+=getPath(start, end, ltr==="z")
start = end
}
return res
};