SIMPLE SOLUTION | USING DFS |
Intuition\n- DFS\n\n# Approach\n- Used for loop and DFS.\n- Loop to move from one instruction to another contained in s and DFS to move from current to deepest 
# Intuition\n- DFS\n\n# Approach\n- Used for loop and DFS.\n- Loop to move from one instruction to another contained in **s** and DFS to move from current to deepest instruction.\n\n\n\n# Code\n```java []\nclass Solution {\n    public int[] executeInstructions(int n, int[] startPos, String s) {\n        int row=startPos[0];\n        int col=startPos[1];\n        int m=s.length();\n        int []results=new int[m];\n        for(int i=0;i<m;i++){\n            results[i]=dfsExecuteInstructions(n,row,col,i,s);\n        }\n        return results;\n    }\n\n    private int dfsExecuteInstructions(int n, int row,int col,int curr, String s) {\n        if(curr>=s.length())\n            return 0;\n        char c=s.charAt(curr);\n        if(c==\'U\')\n            row--;\n        if(c==\'D\')\n            row++;\n        if(c==\'L\')\n            col--;\n        if(c==\'R\')\n            col++;\n        if(row>=n || row<0 || col<0 || col>=n)\n            return 0;\n        int count=1;\n        count+=dfsExecuteInstructions(n,row,col,curr+1,s);\n        return count;\n    }\n}\n```
['Java']