Java simple idea and clean code O(n) Time, O(1) Space
I found the brilliant idea and clean code at here, so I share it.\nThe key observations are same with problem\'s hint:\n- Think of the L and R as people on a ho
I found the brilliant idea and clean code at [here](https://gist.github.com/wangming2046/b5ac650ac565c0e37fd5886b68339201), so I share it.\nThe key observations are same with problem\'s hint:\n- Think of the L and R as people on a horizontal line, where X is a space.\n- L can only go to left spaces, R can only go to right spaces and the people can\'t cross each other.\n\nAssume the index of **first** `L` character in `start` string is `i`, we iterate `end` string to find index `j` of **first**  `L` character in `end`.\nWe can only convert `start` to `end` if  `i <= j`, otherwise it\'s impossible.\nFor example: the following case is okay\nstart = ".....L.."\nend  = "..L....."\n\nThe same thinking when met `R` character in `start`, but because `R` can only go to right spaces, we can only convert `start` to `end` if `i >= j`.\n\nThen, continue iterate `start` string and do same logic.\n\nAlgorithm:\n- Define `j` pointer for `end` string.\n- Iterate `start` string.\n  - When met `L` in `start` string, increase `j` until `end.charAt(j) == \'L\'`, if `j > i` return false\n  - When met `R` in `start` string, increase `j` until `end.charAt(j) == \'R\'`, if `j < i` return false\n\nImplementation:\n\n```java\nclass Solution {\n  \n  public boolean canTransform(String start, String end) {\n    \n    if (start.replace("X", "").length() != end.replace("X", "").length()) {\n      return false;\n    }\n    \n    int len = start.length();\n    int j = 0;\n    for (int i = 0; i < len; i++) {\n      \n      if (start.charAt(i) == \'L\') {\n        // find the next \'L\' position in "end"\n        while (j < len && end.charAt(j) != \'L\') j += 1;\n                \n        // index of next \'L\' in "end" > index of \'L\' in "start", it\'s impossible to convert start to end.\n        // For example:\n        // start = "..L...."\n        // end   = "....L.."\n        if (j > i) return false;\n        \n        j += 1;\n      }       \n      \n      if (start.charAt(i) == \'R\') {\n        // find the next \'R\' position in "end"\n        while (j < len && end.charAt(j) != \'R\') j += 1;\n        \n        // index of next \'R\' in "end" < index of \'R\' in "start", it\'s impossible to convert start to end.\n        // For example:\n        // start = "....R..."\n        // end   = "..R....."\n        if (j < i) return false;\n        \n        j += 1;\n      }      \n    }\n    \n    return true;\n  }\n}\n```
['Two Pointers', 'Java']