C#, Runtime: Beats 100%. Easy to understand.
Complexity

Time complexity:
O(n^2)


Space complexity:
O(m) - Needed to store the result

Code
# Complexity
- Time complexity:
**O(n^2)**

- Space complexity:
**O(m)** - Needed to store the result

# Code
```csharp []
public class Solution {
    public int[] ExecuteInstructions(int n, int[] startPos, string s) {
        var len = s.Length;
        var result = new int[len];
        if (n == 1) return result;
        for (var i = 0; i < len; i++)
        {
            var y = startPos[0];
            var x = startPos[1];
            var countMoves = 0;
            var commandInd = i;
            var moving = true;
            while (commandInd < len && moving)
            {
                var command = s[commandInd];
                switch (command)
                {
                    case 'D':
                        if (y >= n - 1) moving = false;
                        else { y++; countMoves++; }
                        break;
                    case 'U':
                        if (y == 0) moving = false;
                        else { y--; countMoves++; }
                        break;
                    case 'R':
                        if (x >= n - 1) moving = false;
                        else { x++; countMoves++; }
                        break;
                    case 'L':
                        if (x == 0) moving = false;
                        else { x--; countMoves++; }
                        break;
                }
                commandInd++;
            }
            result[i] = countMoves;
        }
        return result;        
    }
}
```
['C#']