C++, Python, Kotlin || Easy Explained Solution || O(n^2)
\n\n\u200E\nC++ :\n\nclass Solution {\npublic:\n    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n        vector<int> ans; //To sav
![image](https://assets.leetcode.com/users/images/4cd060e8-aaff-4d48-a0f2-6db961b1bf90_1656360433.7701964.png)\n\n\u200E\n**C++ :**\n```\nclass Solution {\npublic:\n    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n        vector<int> ans; //To save movements we have done in all iteration\n        int count = 0; //To save movements we have done\n        int ini = startPos[0]; //Saving initial i\n        int inj = startPos[1]; //Saving initial j\n        for(int  i = 0; i < s.length(); i++){\n            for(int  j = i; j < s.length(); j++){ //Iterating over every element with starting index +1 in every iteration\n                if(s[j] == \'R\') startPos[1]++; //If we encounter R move to right i.e. incrementing j value\n                else if(s[j] == \'L\') startPos[1]--;\n                else if(s[j] == \'U\') startPos[0]--;\n                else if(s[j] == \'D\') startPos[0]++;\n                \n                if(startPos[1] > -1 && startPos[1] < n && startPos[0] > -1 && startPos[0] < n) count++; //Breaking loop if we encounter any position outside of grid\n                else j = INT_MAX-1; //-1 becasue while looping again it will make j++ than compare and break loop\n            }\n            ans.push_back(count); //Pushing count in ans because all iterations are done for this condition\n            count = 0; //Ready to count again\n            startPos = {ini, inj}; //Ready at given position\n        }\n        return ans;\n    }\n};\n```\n\u200E\n**Python :**\n```\nclass Solution(object):\n    def executeInstructions(self, n, startPos, s):\n        li = []\n        count = 0;\n        ini = startPos[0]\n        inj = startPos[1]\n        for i in range(len(s)):\n            for j in range(i, len(s)):\n                if s[j] == \'R\' : startPos[1] += 1\n                elif s[j] == \'L\' : startPos[1] -= 1\n                elif s[j] == \'U\' : startPos[0] -= 1\n                elif s[j] == \'D\' : startPos[0] += 1\n                \n                if (startPos[1] > -1 and startPos[1] < n and startPos[0] > -1 and startPos[0] < n) : count += 1\n                else : startPos = [-1,-1] \n            startPos = [ini, inj]\n            li.append(count)\n            count = 0\n        return li\n```\n\u200E\n**Kotlin :**\n```\nclass Solution {\n    fun executeInstructions(n: Int, startPos: IntArray, s: String): IntArray {\n        var li = IntArray(s.length)\n        var count = 0\n        var ini = startPos[0]\n        var inj = startPos[1]\n        \n        var currPosition = startPos\n        \n        for (i in 0..s.length-1){\n            for(j in i..s.length-1){\n                \n                if (s[j] == \'R\') {inj += 1}\n                else if (s[j] == \'L\') {inj -= 1}\n                else if (s[j] == \'U\') {ini -= 1}\n                else if (s[j] == \'D\') {ini += 1}\n                \n                if (inj > -1 && inj < n && ini > -1 && ini < n) count += 1\n                else {\n                    ini = -1\n                    inj = -1\n                }\n            }\n            ini = startPos[0]\n            inj = startPos[1]\n            li[i] = count\n            count = 0\n        }\n        return li\n    }\n}\n```\n\u200E\nIf you think this post was ***helpful***, I will be happy if you give a ***upvote***. Any questions or discussions are welcome! **Thanks a lot.** \uD83D\uDE04
['C', 'Python', 'C++', 'Python3', 'Kotlin']