2 Pointer with boundries ig 😙✌🏽
Intuition\nThe problem involves validating if pieces can be moved to transform start into target while adhering to specific movement constraints for \'L\' and \
# Intuition\nThe problem involves validating if pieces can be moved to transform `start` into `target` while adhering to specific movement constraints for \'L\' and \'R\'. Observing the relative order of \'L\', \'R\', and \'_\' in both strings provides insights for solving the problem.\n\n# Approach\n1. **Simultaneous Iteration**:\n   - Use two pointers `sp` and `tp` to iterate through `start` and `target`, respectively.\n   - Skip over \'_\' in both strings as these positions are neutral.\n\n2. **Validate Pieces**:\n   - Compare characters at `sp` and `tp`:\n     - If one is \'L\' and the other is \'R\', return `false`.\n     - For \'L\', ensure its position in `target` does not exceed its position in `start` because \'L\' can only move left.\n     - For \'R\', ensure its position in `target` does not precede its position in `start` because \'R\' can only move right.\n\n3. **Check Remaining Characters**:\n   - After exiting the loop, ensure no characters other than \'_\' remain in either string. If non-\'_\' characters remain, return `false`.\n\n4. **Return Result**:\n   - If all checks pass, return `true`.\n\n# Complexity\n- **Time Complexity**: \n  $$O(n)$$, where \\(n\\) is the length of the strings. Both strings are traversed at most once.\n  \n- **Space Complexity**: \n  $$O(1)$$, as no additional data structures are used.\n\n# Code\n```java []\nclass Solution {\n    public boolean canChange(String start, String target) {\n\n        int sl = start.length();\n        int tl = target.length();\n\n        int sp = 0;\n        int tp = 0;\n\n        while (sp < sl && tp < tl) {\n            if (start.charAt(sp) == \'_\')\n                sp++;\n            else if (target.charAt(tp) == \'_\')\n                tp++;\n            else if ((start.charAt(sp) == \'L\' && target.charAt(tp) == \'R\') || \n                     (start.charAt(sp) == \'R\' && target.charAt(tp) == \'L\'))\n                return false;\n            else if (start.charAt(sp) == \'L\' && target.charAt(tp) == \'L\') {\n                if (tp > sp) return false;\n\n                sp++;\n                tp++;\n            } else if (start.charAt(sp) == \'R\' && target.charAt(tp) == \'R\') {\n                if (tp < sp) return false;\n\n                sp++;\n                tp++;\n            }\n        }\n\n        // If anything is remaining in any one return false\n        while (sp < sl) {\n            if (start.charAt(sp) != \'_\') return false;\n            sp++;\n        }\n        while (tp < tl) {\n            if (target.charAt(tp) != \'_\') return false;\n            tp++;\n        }\n        return true;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    bool canChange(string start, string target) {\n\n        int sl = start.length();\n        int tl = target.length();\n\n        int sp = 0;\n        int tp = 0;\n\n        while (sp < sl && tp < tl) {\n            if (start[sp] == \'_\')\n                sp++;\n            else if (target[tp] == \'_\')\n                tp++;\n            else if ((start[sp] == \'L\' && target[tp] == \'R\') || \n                     (start[sp] == \'R\' && target[tp] == \'L\'))\n                return false;\n            else if (start[sp] == \'L\' && target[tp] == \'L\') {\n                if (tp > sp) return false;\n\n                sp++;\n                tp++;\n            } else if (start[sp] == \'R\' && target[tp] == \'R\') {\n                if (tp < sp) return false;\n\n                sp++;\n                tp++;\n            }\n        }\n\n        // If anything is remaining in any one return false\n        while (sp < sl) {\n            if (start[sp] != \'_\') return false;\n            sp++;\n        }\n        while (tp < tl) {\n            if (target[tp] != \'_\') return false;\n            tp++;\n        }\n        return true;\n    }\n};\n```\n\n\n\n\n
['C++', 'Java']