✅ Easy to Understand | Two Pointers | Beats 100% | Detailed Video Explanation 🔥
Intuition\nThe problem involves verifying whether a given string start can be transformed into another string target by moving the characters \'L\' and \'R\' un
# Intuition\nThe problem involves verifying whether a given string `start` can be transformed into another string `target` by moving the characters `\'L\'` and `\'R\'` under specific rules:\n- `\'L\'` can only move left.\n- `\'R\'` can only move right.\n- Underscores (`\'_\'`) represent empty spaces.\n\nThe key observation is that the relative order of `\'L\'` and `\'R\'` must remain unchanged, and their movements must respect the rules of directionality.\n\n---\n\n# Approach\n1. Use two pointers (`i` for `start` and `j` for `target`) to traverse both strings simultaneously.\n2. Skip any underscores in both strings, as they don\'t contribute to the logic of character matching.\n3. When encountering characters `\'L\'` or `\'R\'`, check:\n   - The characters must match (`start[i] == target[j]`).\n   - `\'L\'` can only move left, meaning its index in `start` must be greater than or equal to its index in `target`.\n   - `\'R\'` can only move right, meaning its index in `start` must be less than or equal to its index in `target`.\n4. If any of the above conditions fail, return `false`.\n5. After completing the traversal, skip any trailing underscores in both strings.\n6. Ensure both pointers reach the end of their respective strings. If they do, return `true`; otherwise, return `false`.\n\n---\n\n# Complexity\n- **Time complexity:**  \n  $$O(n)$$  \n  We traverse each string at most once, where \\(n\\) is the length of the strings (both assumed to be the same length).\n\n- **Space complexity:**  \n  $$O(1)$$  \n  The algorithm uses constant space for the pointers and comparisons, with no additional data structures.\n\n\n\n# Code\n```java []\nclass Solution {\n    public boolean canChange(String start, String target) {\n        int i = 0, j = 0;\n        int n = start.length(), m = target.length();\n\n        // Traverse both strings using two pointers\n        while (i < n && j < m) {\n            char a = start.charAt(i);\n            char b = target.charAt(j);\n\n            // Skip underscores (\'_\') in both strings\n            if (a == \'_\' && b == \'_\') {\n                i++;\n                j++;\n            } \n            // Skip underscores in the `start` string\n            else if (a == \'_\') {\n                i++;\n            } \n            // Skip underscores in the `target` string\n            else if (b == \'_\') {\n                j++;\n            } \n            // If characters match, ensure valid movement\n            else if (\n                a == b && // Characters must match\n                (\n                    (a == \'L\' && i >= j) || // \'L\' can only move left\n                    (a == \'R\' && i <= j)    // \'R\' can only move right\n                )\n            ) {\n                i++;\n                j++;\n            } \n            // If none of the above conditions are satisfied, return false\n            else {\n                return false;\n            }\n        }\n\n        // Skip remaining underscores in the `start` string\n        while (i < n && start.charAt(i) == \'_\') {\n            i++;\n        }\n\n        // Skip remaining underscores in the `target` string\n        while (j < m && target.charAt(j) == \'_\') {\n            j++;\n        }\n\n        // Both pointers must reach the end for a valid transformation\n        return i == n && j == m;\n    }\n}\n\n```\n\nLeetCode 2337 Move Pieces to Obtain a String | String | Two Pointers | Google Amazon Interview Ques\nhttps://youtu.be/7_i0_o_BUZw
['Two Pointers', 'String', 'Java']