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:
You start at position
• '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!).
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
zYou 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
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
✓ Linear Growth
Space Complexity
O(26)
Hash map to store coordinates of all 26 letters
✓ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code