2 ways to solve this problem: DFS or 2 pointers - Easy to understand solution
Solution 1 (Time Limit Exceeded): Simply DFS, we iterate through all the possible next moves with the current LR string and perform the check, but this solution
**Solution 1 (Time Limit Exceeded)**: Simply DFS, we iterate through all the possible next moves with the current LR string and perform the check, but this solution will cause Time Limit Exceeded:\n```java\nclass Solution {\n    public boolean canTransform(String start, String end) {\n        Map<String, List<String>> map = new HashMap<>();\n        return search(start, end, map);\n    }\n    \n    boolean search(String cur, String end, Map<String, List<String>> map) {\n        if (cur.equals(end)) return true;\n        List<String> states;\n        if (map.containsKey(cur)) states = map.get(cur);\n        else {\n            states = getAllStates(cur);\n            map.put(cur, states);\n        }\n        for (String s : states) {\n            if (search(s, end, map)) return true;\n        }\n        return false;\n    }\n    \n    List<String> getAllStates(String s) {\n        List<String> states = new ArrayList<>();\n        for (int i = 0;i < s.length() - 1;i++) {\n            String ss = s.substring(i, i + 2);\n            if (ss.equals("XL")) {\n                String newS = s.substring(0, i) + "LX" + s.substring(i + 2, s.length());\n                states.add(newS);\n            }\n            \n            if (ss.equals("RX")) {\n                String newS = s.substring(0, i) + "XR" + s.substring(i + 2, s.length());\n                states.add(newS);\n            }\n        }\n        return states;\n    }\n}\n```\n\n**Solution 2 (Accepted)**: Simply use 2 pointers `i, j` to iterate through each character `ci, cj` of both strings one by one:\n- If `ci == \'X\' or cj == \'X\'`, increment `i` or `j`\n- If `ci != cj`, return `false`\n- Else, we will have `ci == cj`, at this step, need to check:\n\t- If `ci == \'L\'`, then we must have `i >= j`, otherwise return `false`\n\t- If `ci == \'R\'`, then we must have `j >= i`, otherwise return `false`\n\nKeep looping to the end of both strings and return `true` at the end of the function.\n```java\nclass Solution {\n    public boolean canTransform(String start, String end) {\n        if (start.length() != end.length()) return false;\n        int n = start.length();\n        int i = 0, j = 0;\n        while (i < n && j < n) {\n            while (i < n && start.charAt(i) == \'X\') i++;\n            while (j < n && end.charAt(j) == \'X\') j++;\n            if (i == n || j == n) break;\n            char s = start.charAt(i);\n            char e = end.charAt(j);\n            if (s != e) return false;\n            if (s == \'L\' && i < j) return false;\n            if (s == \'R\' && i > j) return false;\n            i++;\n            j++;\n        }\n        if (i < n) {\n            if (start.charAt(i) != \'X\') return false;\n            i++;\n        }\n        if (j < n) {\n            if (end.charAt(j) != \'X\') return false;\n            j++;\n        }\n        return true;\n    }\n}\n```
[]