Explanation and java solution
The solution is simple and similar to others, but more detailed explanation is needed.\n\nIn fact, "R" can move to the right until it is blocked by "L" while "L
The solution is simple and similar to others, but more detailed explanation is needed.\n\nIn fact, "R" can move to the right until it is blocked by "L" while "L" can move to the left until it is blocked by "R". \n\nWe may count the number of "L" and "R" as follows. There are several principles during the loop:\n\n1. the value of `l` could be zero and negative, but not positive. This is becasue "L" could move to the left, so "L" may appear earlier in `end` than that in `start`. So once find `l > 0`, we may return false;\n2. the value of `r` could be zero and positive, but not negative. The reasons are similar to the one above. So once find`r < 0`, we may return false;\n3. When `l < 0`, `r` must be zero. This is because when `l < 0`, it means that one "L" appears earlier in `end`, and all "R"s before the current "L" in `end` should have also been visited in `start` (this is because "R" could only remove to the right).\n4.  When `r > 0`, `l` must be zero. Reasons are similar.\n```java\nclass Solution {\n    public boolean canTransform(String start, String end) {\n        int l = 0, r = 0;\n        for (int i = 0; i < start.length(); i++) {\n            if (start.charAt(i) == \'L\') l++;\n            if (start.charAt(i) == \'R\') r++;\n            if (end.charAt(i) == \'L\') l--;\n            if (end.charAt(i) == \'R\') r --;\n            \n            if (l > 0 || r < 0 || ( l < 0 && r != 0) || (r > 0 && l != 0)) return false;\n        }\n        \n        return l == 0 && r == 0;\n    }\n}\n```
[]