🔥Beats 100 % 🔥 | 11 MS | Tow Pointers | ✔️ Explained & Optimized Solution ✔️ |
\n\n---\n\n# Intuition\nThe idea is to use Two Pointers i and j to traverse the start and target strings simultaneously while skipping _ characters. For each ch
![image.png](https://assets.leetcode.com/users/images/11c7923b-26fb-4c9a-8db9-5af81da22449_1733368010.851549.png)\n\n---\n\n# Intuition\nThe idea is to use **Two Pointers** `i` and `j` to traverse the `start` and `target` strings simultaneously while skipping `_` characters. For each characterEnsure both pointers are aligned on valid characters `L` or `R`.Check whether the character alignment violates movement constraints: `\'L\'` can only move to the **left** `\'R\'` can only move to the **right**. If all conditions are satisfied, the transformation is **valid**.\n\n# Approach\n`Step 1` **Convert start and target strings into character arrays (s and t).**\n\n`Step 2` **Initialize two pointers i and j at the start of both arrays.**\n\n`Step 3` **While Traversing :**\n- Skip `_` characters in both Arrays :\n    - Increment `i` until `s[i]` points to a non-blank `\'L\'` or `\'R\'` or reaches the end.\n    - Increment `j` until `t[j]` points to a non-blank or reaches the end.\n- If either pointer reaches the end:\n    - Ensure both pointers have reached the end simultaneously \n    `return false otherwise`.\n\n`Step 4` **For each pair of aligned Characters :**\n- Ensure the characters match `s[i] == t[j]`.\n- Check movement constraints:\n    - `\'L\'` can only move to the left `i >= j`.\n    - `\'R\'` can only move to the right `i <= j`.\n\n`Step 5` **If all characters align correctly, return true. Otherwise, return false**.\n\n# Complexity\n- Time complexity : $$O(n)$$\n- Space complexity : $$O(n)$$\n\n# Code\n```java []\nclass Solution\n{\n    public boolean canChange(String start, String target)\n    {\n        // Step 1: Convert strings to character arrays for traversal\n        char[] s = start.toCharArray();\n        char[] t = target.toCharArray();\n        int n = s.length;\n        int i = 0, j = 0; // Pointers for `start` and `target` arrays\n\n        // Step 2: Traverse both arrays using pointers\n        while (i <= n && j <= n)\n        {\n\n            // Step 3: Skip blank spaces (\'_\') in both arrays\n            while (i < n && s[i] == \'_\') i++;\n            while (j < n && t[j] == \'_\') j++;\n\n            // Step 4: If either pointer reaches the end, ensure both are at the end\n            if (i == n || j == n) return i == n && j == n;\n\n            // Step 5: Compare aligned characters\n            if (s[i] != t[j]) return false;\n\n            // Step 6: Check movement constraints\n            if (s[i] == \'L\' && i < j) return false; // \'L\' can only move to the left\n            if (s[i] == \'R\' && i > j) return false; // \'R\' can only move to the right\n\n            // Step 7: Move both pointers to the next character\n            i++;\n            j++;\n        }\n\n        // Step 8: If all checks pass, the transformation is valid\n        return true;\n    }\n}\n\n```
['Two Pointers', 'String', 'Java']