Simple Java solution | Keep ordering of L, R
The idea is must guarantee:\n- Number of L, R are the same and keep ordering. \n- Number of X on the left of L from end always less than or equal the correspond
The idea is must guarantee:\n- Number of L, R are the same and keep ordering. \n- Number of X on the left of L from `end` always less than or equal the corresponding L from `start`\n- Number of X on the right of R from `end` always more than or equal the corresponding R from `start`\n\nWe can terminate the process early once got the first violation, no need to loop through the end of `start`.\n\n# Complexity\n- Time complexity: `O(n)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(n)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public boolean canTransform(String start, String end) {\n        Queue<Node> logs = new LinkedList<>();\n\n        int count = 0;\n        for (char c : start.toCharArray()) {\n            if (c == \'X\') count++;\n            else {\n                logs.add(new Node(c, count));\n            }\n        }\n\n        count = 0;\n        for (char c : end.toCharArray()) {\n            if (c == \'X\') count++;\n            else {\n                if (logs.isEmpty()) return false;\n\n                Node node = logs.poll();\n                if (c != node.c) return false;\n\n                if (c == \'L\' && count > node.count) return false;\n                if (c == \'R\' && count < node.count) return false;                \n            }\n        }\n\n        return logs.isEmpty();\n    }\n\n    class Node {\n        public Character c;\n        public int count;\n\n        public Node(Character c, int count) {\n            this.c = c;\n            this.count = count;\n        }\n    }\n}\n```
['Java']