Alphabet Board Path - Problem
Navigate the Alphabet Board!

Imagine you have a virtual keyboard laid out as a grid where each letter of the alphabet occupies a specific position. Your task is to navigate this board and spell out any given word by moving from letter to letter.

The alphabet board looks like this:
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


You start at position (0, 0) which contains the letter 'a'. You can move in four directions:
'U' - Move up one row
'D' - Move down one row
'L' - Move left one column
'R' - Move right one column
'!' - Select the current letter

Your goal is to return a sequence of moves that spells out the target word using the minimum number of moves. Note that you can only move to positions that actually contain letters (so be careful with 'z' which is alone on the bottom row!).

Input & Output

example_1.py — Basic Word
$ Input: target = "leet"
Output: "DDR!UURRR!DDDR!UUL!"
💡 Note: Starting at 'a' (0,0): Go to 'l' (2,1) with DD R!, then to first 'e' (0,4) with UU RRR!, then to second 'e' (0,4) already there!, then to 't' (3,4) with DDD R!, then to 't' (3,3) with U L!
example_2.py — Single Character
$ Input: target = "code"
Output: "RR!R!DD!"
💡 Note: From 'a' to 'c': RR!, from 'c' to 'o': DDRR!, from 'o' to 'd': UUL!, from 'd' to 'e': R!
example_3.py — Edge Case with Z
$ Input: target = "zdz"
Output: "DDDDD!UUUUURRRRR!LLLLLDDDDDD!"
💡 Note: From 'a' to 'z': DDDDD! (down 5 rows), from 'z' to 'd': UUUUR! (up first to avoid bounds, then right), from 'd' to 'z': LLLLLDDDDD! (left first, then down to 'z')

Visualization

Tap to expand
🎯 Alphabet Board Navigation StrategyThe Alphabet Board:a(0,0)bcdefghijkl(2,1)mnoz(5,0)RDD!🧭 Navigation Strategy1. Pre-compute Coordinates:• Build hash map: 'a'→(0,0), 'b'→(0,1), ... 'z'→(5,0)• O(1) lookup instead of O(26) search2. Manhattan Distance:• Distance = |row₂ - row₁| + |col₂ - col₁|• Example: (0,0) to (2,1) = |2-0| + |1-0| = 3 moves3. Movement Order (Critical!):Going TO 'z':• Move LEFT first, then DOWN• Avoids out-of-bounds positionsLeaving FROM 'z':• Move UP first, then horizontal• Then DOWN to final position4. Generate Path:• Convert distance to UDLR moves + '!' selection💡 Key Insight:This is essentially a coordinate geometry problem disguised as a board navigation puzzle!The 'z' edge case teaches us about constraint handling in pathfinding algorithms.
Understanding the Visualization
1
Map the Territory
Create a coordinate system where each letter has a fixed (row, col) position
2
Plan the Route
For each target character, calculate the shortest Manhattan distance path
3
Handle Edge Cases
Special navigation rules for 'z' since it's isolated on the bottom row
4
Execute Moves
Generate the sequence of U/D/L/R moves and '!' selections
Key Takeaway
🎯 Key Insight: Transform the problem into coordinate geometry with pre-computed positions for O(1) lookup, handle the 'z' edge case with proper move ordering to avoid out-of-bounds positions.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We process each character in target string once, with O(1) lookup per character

n
2n
Linear Growth
Space Complexity
O(26)

Hash map to store coordinates of all 26 letters

n
2n
Linear Space

Constraints

  • 1 ≤ target.length ≤ 100
  • target consists only of English lowercase letters
  • The board layout is fixed as shown: ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
  • Special constraint: 'z' is the only character on the 6th row, so movement must be carefully ordered
Asked in
Facebook 15 Google 12 Amazon 8 Microsoft 6
23.1K 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