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"].

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.

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

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

Example 1
Inputtarget = "leet"
Output"DDR!UURRR!!DDD!"
Following these moves visits and appends the letters l, e, e, and t in order.
Example 2
Inputtarget = "code"
Output"RR!DDRR!UUL!R!"
Following these moves visits and appends the letters c, o, d, and e in order.

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate