1138. Alphabet Board Path

Leetcode 問題: 1138. Alphabet Board Path

題目

On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = [“abcde”, “fghij”, “klmno”, “pqrst”, “uvwxy”, “z”], as shown in the diagram below.

Leetcode

根據字母版上面的路徑,移動字母,把傳入的字串拼完

答案

JavaScript

/**
 * @param {string} targetSentence
 * @return {string}
 */
var alphabetBoardPath = function(targetSentence) {

    // 字母移動路徑
    let letterMovingPathMapping = {};
    let movingLeft = 'L';
    let movingRight = 'R';
    let movingUp = 'U';
    let movingDown = 'D';


    for (let beginLetter = 0; beginLetter < 26 ; beginLetter++ ) {
        // 初始化字母對應表
        letterMovingPathMapping[beginLetter] = {};
        for (let targetLetter = 0; targetLetter < 26 ; targetLetter++ ) {
            // 要移動的 X 距離 : 原始字母在那一層 X 位置 - 目標字母在那一層 X 位置
            //  - movingX > 0 : 往右移動 R
            //  - movingX < 0 : 往左移動 L
            let movingX = Math.floor(targetLetter % 5 - beginLetter % 5);
            // 要移動的 Y 距離 : 原始字母在那一層 Y - 目標字母在那一層 Y
            //  - movingY > 0 : 往下移動 D
            //  - movingY < 0 : 往上移動 U
            let movingY = Math.floor(targetLetter / 5) - Math.floor(beginLetter / 5);

            // 移動路徑
            let movingPath = '';
            // 移動路徑 Y 剩餘路徑
            let movingYLeftPath = '';


            if (movingY < 0 ) {
                //  - movingY < 0 : 往上移動 U
                let movingStep = Math.abs(movingY);
                movingPath += movingUp.repeat(movingStep);
            }
            if (movingY > 0 ) {
                //  - movingY > 0 : 往下移動 D
                let movingStep = movingY;
                if (targetLetter == 25) {
                    // 移動到 Z,最後一步不要移動,會跑到空位置,留到最後一步再動
                    movingStep--;
                    movingYLeftPath = movingDown;
                }
                movingPath += movingDown.repeat(movingStep);
            }

            if (movingX < 0 ) {
                //  - movingX < 0 : 往左移動 L
                let movingStep = Math.abs(movingX);
                movingPath += movingLeft.repeat(movingStep);
            }
            if (movingX > 0 ) {
                //  - movingX > 0 : 往右移動 R
                let movingStep = movingX;
                movingPath += movingRight.repeat(movingStep);
            }

            // 加上最後一步要移動的 Y
            movingPath += movingYLeftPath;
            // 設定字母對應路徑
            letterMovingPathMapping[beginLetter][targetLetter] = movingPath;
        }
    }

    // 移動路徑
    let pathResult = '';
    // 字母 a 索引
    let baseLetterCode = 'a'.charCodeAt(0);
    // 起始字母索引
    let beginLetterIndex = 'a'.charCodeAt(0) - baseLetterCode;
    // 轉換成小寫
    let lowerCaseTargetSentence = targetSentence.toLowerCase();

    for (let i in lowerCaseTargetSentence) {
        // 目標字母索引
        let targetLetterIndex = targetSentence[i].charCodeAt(0) - baseLetterCode;
        // 設定對應路徑
        pathResult += letterMovingPathMapping[beginLetterIndex][targetLetterIndex] + '!';
        // 將目前指標位置設定為當前字母
        beginLetterIndex = targetLetterIndex;
    }

    return pathResult;
};

參考資料