JAVA | | BruteForce--;
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int[] executeInstructions(int n, int[] startPos, String s) {
        int[] res = new int[s.length()];
        int left = startPos[0];
        int right = startPos[1];
        for (int i = 0; i < s.length(); i++) {
            startPos[0]=left;
            startPos[1]=right;
            res[i] = count(n, startPos, s.substring(i, s.length()));
        }
        return res;
    }

    public int count(int n, int[] p, String s) {
        int count = 0;
        boolean flag = true;
        for (char c : s.toCharArray()) {
            if (c == 'R') {
                int i = p[0], j = p[1] + 1;
                if (i < 0 || i > n - 1 || j < 0 || j > n - 1) {
                    flag = false;
                    break;
                } else {
                    p[0]=i;
                    p[1]=j;
                }
            } else if (c == 'L') {
                int i = p[0], j = p[1] - 1;
                if (i < 0 || i > n - 1 || j < 0 || j > n - 1) {
                    flag = false;
                    break;
                } else {
                    p[0]=i;
                    p[1]=j;
                }
            } else if (c == 'D') {
                int i = p[0]+1, j = p[1];
                if (i < 0 || i > n - 1 || j < 0 || j > n - 1) {
                    flag = false;
                    break;
                } else {
                    p[0]=i;
                    p[1]=j;
                }
            } else {
                int i=p[0]-1, j = p[1];
                if (i < 0 || i > n - 1 || j < 0 || j > n - 1) {
                    flag = false;
                    break;
                } else {
                    p[0]=i;
                    p[1]=j;
                }
            }
            count++;
        }
        return count;
    }
}
```
['Java']