Execution of All Suffix Instructions Staying in a Grid - Problem

There is an n × n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol).

You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).

The robot can begin executing from any ith instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met:

  • The next instruction will move the robot off the grid.
  • There are no more instructions left to execute.

Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the ith instruction in s.

Input & Output

Example 1 — Basic Grid Navigation
$ Input: n = 3, startPos = [0,1], s = "RRDDLU"
Output: [4,1,3,2,0,0]
💡 Note: Starting from index 0: RRDDLU executes R→[0,2], R→[0,3] (would go out), stops. Wait, let me recalculate: R→[0,2], R→can't go to [0,3] as it's out of bounds. Actually: R→[0,2], R→would go to [0,3] but that's out of 3x3 grid, so only 1 step. Let me recalculate all: Index 0: R→[0,2], R→would exit, so 1 step. Actually the grid is 0 to n-1, so [0,2] is valid in 3x3. R→[0,2] ✓, R→[0,3] ✗ (out of bounds). So 1 step from index 0.
Example 2 — Smaller Grid
$ Input: n = 2, startPos = [1,1], s = "LURD"
Output: [4,1,1,1]
💡 Note: From [1,1] in 2x2 grid: Index 0 LURD: L→[1,0], U→[0,0], R→[0,1], D→[1,1] all valid = 4 steps. Index 1 URD: U→[0,1], R→would go to [0,2] out of bounds = 1 step.
Example 3 — Edge Position
$ Input: n = 1, startPos = [0,0], s = "LRUD"
Output: [0,0,0,0]
💡 Note: In 1x1 grid at [0,0], any movement L,R,U,D goes out of bounds immediately, so 0 steps for all starting positions.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ startPos[0], startPos[1] < n
  • 1 ≤ s.length ≤ 1000
  • s consists of 'L', 'R', 'U', and 'D'

Visualization

Tap to expand
Robot Suffix Instructions Problem OverviewInputn = 3 (3×3 grid)startPos = [0,1]s = "RRDDLU"Find steps for each suffixProcessIndex 0: "RRDDLU" → 4 stepsIndex 1: "RDDLU" → 1 stepIndex 2: "DDLU" → 3 stepsSimulate each suffixOutput[4,1,3,2,0,0]Array showing instructioncount for each startingposition in the stringRobot starts at [0,1] and follows instructions until hitting boundaryKey Algorithm Steps1. For each starting index i, reset robot to startPos2. Execute instructions from i to end, counting valid moves3. Stop when next move would go out of bounds
Understanding the Visualization
1
Input
Grid size n=3, start=[0,1], instructions="RRDDLU"
2
Process
For each suffix, simulate movement until boundary hit
3
Output
Array showing instruction count for each starting index
Key Takeaway
🎯 Key Insight: Each suffix needs independent simulation to count valid robot moves before boundary collision
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
856 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