O(n) time O(1) space C++
Let j be the index that is the first that r[j] != r[i]. Moving i from 0 to n, if r[i] == s[i] then do nothing. When r[i] != s[i], it is only feasible to change 
Let j be the index that is the first that r[j] != r[i]. Moving i from 0 to n, if r[i] == s[i] then do nothing. When r[i] != s[i], it is only feasible to change r to match s if r[i] == 'R' or s[i] == 'L', and one of them is 'X'. In that case the right move is to move r[j] all the way back to i-th position in case r[i] = 'X' and r[j] ='L', or to move r[i] all the way to j-th position if r[i] ='R' and r[j] = 'X'. Either case is equivalent to swap(r[i], r[j]).\n```\nclass Solution {\npublic:\n    bool can(string r, string s) {\n        int n = r.size();\n        r += 'T';\n        int i = 0, j = 0;\n        while (i < n) {\n            while (r[j] == r[i]) ++j;\n            if (i >= 0 && s[i] != r[i]) {\n                if (r[i] != 'X' && s[i] != 'X') return false;\n                if (r[i] == 'R' || s[i] == 'L') {\n                    if (r[j] != s[i]) return false;\n                    swap(r[j],r[i]);\n                }\n                else return false;\n            }\n            ++i;\n        }\n        return true;\n    }    \n    \n    bool canTransform(string r, string s) {\n        if (r.size() != s.size()) return false;\n        return can(r,s);\n    }\n};\n```
[]