Python | Two-Pointer Pattern
see the Successfully Accepted Submission\n\n# Code\npython3 []\nclass Solution:\n    def canChange(self, start: str, target: str) -> bool:\n        # Remove \'_
[see the Successfully Accepted Submission](https://leetcode.com/problems/move-pieces-to-obtain-a-string/submissions/1470587258/?envType=daily-question&envId=2024-12-05)\n\n# Code\n```python3 []\nclass Solution:\n    def canChange(self, start: str, target: str) -> bool:\n        # Remove \'_\' characters and check if the relative order of \'L\' and \'R\' is the same\n        if start.replace(\'_\', \'\') != target.replace(\'_\', \'\'):\n            return False\n\n        # Check the relative positions of \'L\' and \'R\'\n        start_L_positions = []\n        start_R_positions = []\n        target_L_positions = []\n        target_R_positions = []\n\n        for i in range(len(start)):\n            if start[i] == \'L\':\n                start_L_positions.append(i)\n            elif start[i] == \'R\':\n                start_R_positions.append(i)\n            if target[i] == \'L\':\n                target_L_positions.append(i)\n            elif target[i] == \'R\':\n                target_R_positions.append(i)\n\n        # Check L positions (should only move to the left)\n        for s_pos, t_pos in zip(start_L_positions, target_L_positions):\n            if s_pos < t_pos:\n                return False\n\n        # Check R positions (should only move to the right)\n        for s_pos, t_pos in zip(start_R_positions, target_R_positions):\n            if s_pos > t_pos:\n                return False\n\n        return True\n\n```\n\n\n# Explanation:\n\n### Remove Blanks and Validate Order:\n- Remove the `_` characters from both strings and check if the remaining characters (`L` and `R`) are in the same order in both `start` and `target`.\n- If not, it\'s impossible to transform `start` into `target`.\n\n### Track Positions of `L` and `R`:\n- Extract the indices of `L` and `R` from both `start` and `target`.\n\n### Check Constraints:\n1. For each `L` in `start`, ensure its target position is not to the right (as `L` can only move left).\n2. For each `R` in `start`, ensure its target position is not to the left (as `R` can only move right).\n\n### Return True or False:\n- Return `True` only if all constraints are satisfied; otherwise, return `False`.\n\n---\n\n## Complexity:\n\n- **Time Complexity**: \\(O(n)\\), where \\(n\\) is the length of the string, since we iterate over the strings a constant number of times.\n- **Space Complexity**: \\(O(n)\\) for the position lists and intermediate strings.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']