Watch 7 video solutions for Alphabet Board Path, a medium level problem involving Hash Table, String. This walkthrough by Inside code has 6,744 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.(Here, 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.
Example 1:
Input: target = "leet" Output: "DDR!UURRR!!DDD!"
Example 2:
Input: target = "code" Output: "RR!DDRR!UUL!R!"
Constraints:
1 <= target.length <= 100target consists only of English lowercase letters.Problem Overview: You are given an alphabet board where characters 'a' to 'y' form a 5x5 grid and 'z' sits alone on the next row. Starting at 'a', you must generate the sequence of moves (U, D, L, R) that navigates the board and presses ! for each character in a target string.
Approach 1: Direct Coordinate Mapping (Time: O(n), Space: O(1))
The board position of each letter is deterministic, so you can map every character to a coordinate (row, col). Iterate through the target string and compute the difference between the current position and the next character’s position. Move vertically and horizontally using repeated U/D/L/R operations, then append ! to select the character. The only tricky case is 'z', which sits at (5,0). When moving toward or away from 'z', prioritize up or left moves before down or right to avoid stepping into invalid cells of the board. The algorithm processes each character once, performing constant work per transition. Time complexity is O(n), where n is the target length, and space complexity is O(1) excluding the output string. This approach relies on simple coordinate math and works well with problems involving hash table lookups or direct character indexing.
Approach 2: Divide and Conquer Path Construction (Time: O(n), Space: O(log n) recursion)
This strategy treats the path generation problem as smaller independent segments. Split the target string into halves, recursively compute the path for each segment, then combine them while preserving the correct board position transitions. Each recursive step still uses coordinate differences to generate the path between characters, but the recursion structure allows modular path construction. While this does not improve asymptotic complexity, it can make implementations easier to reason about when composing path segments or integrating additional constraints. The algorithm still processes every character once, so the total time complexity remains O(n). Recursion introduces stack overhead, leading to O(log n) or O(n) auxiliary space depending on the split strategy. String manipulation is the main operation, making this approach closely tied to string processing patterns.
Recommended for interviews: Direct Coordinate Mapping is the expected solution. Interviewers want to see that you model the board with coordinates and carefully handle the 'z' edge case. A brute-force style navigation shows basic reasoning, but the coordinate-based approach demonstrates strong control over grid movement and constant-time character mapping.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Coordinate Mapping | O(n) | O(1) | Best general solution. Efficient when board coordinates can be computed directly. |
| Divide and Conquer Path Construction | O(n) | O(log n) recursion | Useful when building paths in modular segments or experimenting with recursive string construction. |