Simple & Easy to understand solution
\n\n# Code\n\nclass Solution {\npublic:\n    int countInstructions(int n,vector<int>&startPos,string s,int i){\n        int cnt=0;\n        int r=startPos[0],c=
\n\n# Code\n```\nclass Solution {\npublic:\n    int countInstructions(int n,vector<int>&startPos,string s,int i){\n        int cnt=0;\n        int r=startPos[0],c=startPos[1];\n        for(int p=i;p<s.length();p++){\n            if(s[p]==\'R\'){\n                if(c+1<n){\n                    cnt++;\n                    c++;\n                }\n                else break;\n            }\n            else if(s[p]==\'L\'){\n                if(c-1>=0){\n                    cnt++;\n                    c--;\n                }\n                else break;\n            }\n            else if(s[p]==\'U\'){\n                if(r-1>=0){\n                    cnt++;\n                    r--;\n                }\n                else break;\n            }\n            else{\n                if(r+1<n){\n                    r++;\n                    cnt++;\n                }\n                else break;\n            }\n        }\n        return cnt;\n    }\n\n    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n        vector<int>res;\n        for(int i=0;i<s.length();i++){\n            res.push_back(countInstructions(n,startPos,s,i));\n        }\n        return res;\n        \n    }\n};\n```
['C++']