Alphabet Board Path - Problem

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.

We may make the following moves:

  • 'U' moves our position up one row, if the position exists on the board
  • 'D' moves our position down one row, if the position exists on the board
  • 'L' moves our position left one column, if the position exists on the board
  • 'R' moves our position right one column, if the position exists on the board
  • '!' adds the character board[r][c] at our current position (r, c) to the answer

Note: The only positions that exist on the board are positions with letters on them.

Return a sequence of moves that makes our answer equal to target in the minimum number of moves. You may return any path that does so.

Input & Output

Example 1 — Basic Navigation
$ Input: target = "leet"
Output: "DDR!UURRR!!DDD!"
💡 Note: Start at 'a'(0,0). Go to 'l'(2,1): DD(down 2)R(right 1)!. Go to 'e'(0,4): UU(up 2)RRR(right 3)!. Go to 'e'(0,4): already there!. Go to 't'(3,4): DDD(down 3)!
Example 2 — Edge Case with 'z'
$ Input: target = "zdz"
Output: "DDDDD!UUUURRR!DDDDD!"
💡 Note: Start at 'a'(0,0). Go to 'z'(5,0): DDDDD(down 5)!. Go to 'd'(0,3): UUUUU(up 5)RRR(right 3)!. Go to 'z'(5,0): DDDDD(down 5)!
Example 3 — Single Character
$ Input: target = "a"
Output: "!"
💡 Note: Already at 'a'(0,0), just add '!' to collect the character

Constraints

  • 1 ≤ target.length ≤ 100
  • target consists only of English lowercase letters.

Visualization

Tap to expand
Alphabet Board Path INPUT Alphabet Board (5x6 grid) a b c d e f g h i j k l m n o p q r s t u v w x y z S Target String: "leet" Start: (0,0) = 'a' Moves: U, D, L, R, ! ! = add character ALGORITHM STEPS 1 Build Coordinate Map Map each char to (row, col) 'l' --> (2,1) 'e' --> (0,4) 't' --> (3,4) 2 Calculate Distance Find row/col difference dr = target_row - curr_row dc = target_col - curr_col 3 Move Priority: L/U first Handle 'z' corner case Go L/U before D/R (avoids going off grid at 'z') 4 Add Move Sequence Append moves + '!' char 'a'-->'l': DDR! (2 down, 1 right) 'l'-->'e': UURRR! (2 up, 3 right) 'e'-->'e': ! (same position) 'e'-->'t': DDDL! (3 down, 1 left) FINAL RESULT Movement Path on Board: l e t a Move Breakdown: 1. 'l': DDR! (3 moves) 2. 'e': UURRR! (5 moves) 3. 'e': ! (0 moves) 4. 't': DDDL! (4 moves) Total: 12 moves + 4 '!' Output: "DDR!UURRR!!DDDL!" Key Insight: Use a hash map to store coordinates (row, col) for each character. Calculate the difference between current and target positions. Move LEFT/UP before RIGHT/DOWN to handle the 'z' corner case (row 5 only has one character). Time: O(26 + n*max_dist), Space: O(26). TutorialsPoint - Alphabet Board Path | Hash Map with Coordinate Mapping
Asked in
Google 25 Facebook 18 Amazon 15
28.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen