python/java easy to understand
\tclass Solution:\n\t\tdef executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:\n\t\t\tdirections = {"L": (0, -1), "R": (0, 1), "U": (-1
\tclass Solution:\n\t\tdef executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:\n\t\t\tdirections = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)}\n\t\t\tres = []\n\t\t\tfor i in range(len(s)):\n\t\t\t\tx, y = startPos\n\t\t\t\tcnt = 0\n\t\t\t\tfor j in range(i, len(s)):\n\t\t\t\t\tx += directions[s[j]][0]\n\t\t\t\t\ty += directions[s[j]][1]\n\t\t\t\t\tif 0 <= x < n and 0 <= y < n:\n\t\t\t\t\t\tcnt += 1\n\t\t\t\t\telse:\n\t\t\t\t\t\tbreak\n\t\t\t\tres.append(cnt)\n\t\t\treturn res\n\t\t\t\n\t\t\t\n\tclass Solution {\n\t\tpublic int[] executeInstructions(int n, int[] startPos, String s) {\n\t\t\tMap<Character, int[]> directions = new HashMap<>();\n\t\t\tdirections.put(\'L\', new int[] {0, -1});\n\t\t\tdirections.put(\'R\', new int[] {0, 1});\n\t\t\tdirections.put(\'U\', new int[] {-1, 0});\n\t\t\tdirections.put(\'D\', new int[] {1, 0});\n\t\t\tint[] res = new int[s.length()];\n\t\t\tint i = 0;\n\t\t\tfor (int j = 0; j < s.length(); j ++) {\n\t\t\t\tint x = startPos[0];\n\t\t\t\tint y = startPos[1];\n\t\t\t\tint cnt = 0;\n\t\t\t\tfor (int k = j; k < s.length(); k ++) {\n\t\t\t\t\tx += directions.get(s.charAt(k))[0];\n\t\t\t\t\ty += directions.get(s.charAt(k))[1];\n\t\t\t\t\tif (0 <= x && x < n && 0 <= y && y < n) {\n\t\t\t\t\t\tcnt ++;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tres[i] = cnt;\n\t\t\t\ti ++;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\t}
[]