Simple and Easy Solution | ✅Beats 100% | C++| Java | Python | JavaScript
\u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n## Connect with me on Linkedin Bijoy Sing.  \n\n## Follow me also on Codeforces: Bijoy Sing  \n\n###### Solution 
# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n## Connect with me on Linkedin [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/).  \n\n## Follow me also on Codeforces: [Bijoy Sing](https://codeforces.com/profile/BijoySingh7)  \n\n###### *Solution in C++, Python, Java, and JavaScript*  \n\n```cpp []\nclass Solution {\npublic:\n    bool canChange(string start, string target) {\n        int left = 0, right = 0;\n\n        for (int i = 0; i < start.size(); i++) {\n            if (start[i] == \'R\') {\n                right++;\n                if (left != 0)\n                    return false;\n            } else if (start[i] == \'L\')\n                left--;\n            if (target[i] == \'R\')\n                right--;\n            else if (target[i] == \'L\') {\n                left++;\n                if (right != 0)\n                    return false;\n            }\n            if (left < 0 || right < 0)\n                return false;\n        }\n        return left == 0 and right == 0;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def canChange(self, start: str, target: str) -> bool:\n        left = 0\n        right = 0\n\n        for i in range(len(start)):\n            if start[i] == \'R\':\n                right += 1\n                if left != 0:\n                    return False\n            elif start[i] == \'L\':\n                left -= 1\n            if target[i] == \'R\':\n                right -= 1\n            elif target[i] == \'L\':\n                left += 1\n                if right != 0:\n                    return False\n            if left < 0 or right < 0:\n                return False\n\n        return left == 0 and right == 0\n```\n\n```java []\nclass Solution {\n    public boolean canChange(String start, String target) {\n        int left = 0, right = 0;\n\n        for (int i = 0; i < start.length(); i++) {\n            if (start.charAt(i) == \'R\') {\n                right++;\n                if (left != 0)\n                    return false;\n            } else if (start.charAt(i) == \'L\') {\n                left--;\n            }\n            if (target.charAt(i) == \'R\') {\n                right--;\n            } else if (target.charAt(i) == \'L\') {\n                left++;\n                if (right != 0)\n                    return false;\n            }\n            if (left < 0 || right < 0)\n                return false;\n        }\n        return left == 0 && right == 0;\n    }\n}\n```\n\n```javascript []\nclass Solution {\n    canChange(start, target) {\n        let left = 0, right = 0;\n\n        for (let i = 0; i < start.length; i++) {\n            if (start[i] === \'R\') {\n                right++;\n                if (left !== 0)\n                    return false;\n            } else if (start[i] === \'L\') {\n                left--;\n            }\n            if (target[i] === \'R\') {\n                right--;\n            } else if (target[i] === \'L\') {\n                left++;\n                if (right !== 0)\n                    return false;\n            }\n            if (left < 0 || right < 0)\n                return false;\n        }\n        return left === 0 && right === 0;\n    }\n}\n```\nCertainly! Let\u2019s expand the explanation further to make the approach and intuition clearer:\n\n---\n\n# Intuition  \nThe problem asks us to transform a `start` string into a `target` string by moving the characters \'R\' (right) and \'L\' (left), with specific movement restrictions:\n- An \'R\' can only move rightwards (i.e., it cannot be placed in a position where it would be to the left of its original position).\n- An \'L\' can only move leftwards (i.e., it cannot be placed in a position where it would be to the right of its original position).\nThus, our goal is to check if we can rearrange the characters in `start` to match those in `target`, while adhering to these movement restrictions.\n\nThe key challenge is ensuring that:\n- The relative positions of \'R\' and \'L\' remain valid.\n- If \'R\' appears before \'L\' in the `start` string, it should also appear before \'L\' in the `target` string, and vice versa. This ensures that the characters maintain their relative order.\n\n# Approach  \nWe iterate through the `start` and `target` strings simultaneously. At each step, we check:\n- If we encounter an \'R\' in `start`, we ensure it can move right to its corresponding position in `target`.\n- Similarly, if we encounter an \'L\' in `start`, we ensure it can move left to its corresponding position in `target`.\n- We also keep track of the positions of \'R\' and \'L\' to ensure that they maintain the required order as they move.\n\nHere are the main conditions we check:\n1. If at any point, an \'R\' needs to move left (i.e., the number of right-moving \'R\' exceeds the number of \'R\' in `target`), return `false`.\n2. If at any point, an \'L\' needs to move right (i.e., the number of left-moving \'L\' exceeds the number of \'L\' in `target`), return `false`.\n3. Finally, at the end of the loop, we check if the counts of \'R\' and \'L\' match in both strings, ensuring that we have the same number of characters in both strings, and no illegal movement occurred.\n---\n\n### Example Walkthrough:\n\n#### Example 1:\nInput:  \n`start = "_L__R__R_"`,  \n`target = "L______RR"`.\n\n**Execution**:\n- `i = 0`: `start[0] = \'_\'`, `target[0] = \'L\'`, update `left++` \u2192 `left = 1`.\n- `i = 1`: `start[1] = \'L\'`, `target[1] = \'_\'`, update `left--` \u2192 `left = 0`.\n- `i = 2 to 6`: Both `start[i]` and `target[i]` are `\'_\'`.\n- `i = 7`: `start[7] = \'R\'`, `target[7] = \'_\'`, update `right++` \u2192 `right = 1`.\n- `i = 8`: `start[8] = \'R\'`, `target[8] = \'R\'`, update `right--` \u2192 `right = 0`.\n\nFinal check: `left == 0` and `right == 0` \u2192 Return `true`.\n\n---\n\n#### Example 2:\nInput:  \n`start = "R_L_"`,  \n`target = "__LR"`.\n\n**Execution**:\n- `i = 0`: `start[0] = \'R\'`, `target[0] = \'_\'`, update `right++` \u2192 `right = 1`.\n- `i = 1`: `start[1] = \'_\'`, `target[1] = \'_\'`.\n- `i = 2`: `start[2] = \'L\'`, `target[2] = \'L\'`, update `left--` \u2192 `left = -1`.\n\nAt `i = 2`, `left < 0` \u2192 Return `false`.\n\n---\n\n# Complexity  \n\n- **Time complexity**:  \n  $$O(n)$$, where $$n$$ is the length of the string. This is because we only traverse the strings once, performing constant-time operations for each character.\n\n- **Space complexity**:  \n  $$O(1)$$, as we only use a few integer counters (`left` and `right`) to keep track of the movements of \'L\' and \'R\'. We do not use any extra data structures that scale with the size of the input.\n\n---\n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*
['Array', 'Two Pointers', 'String', 'String Matching', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']