CPP | Two Pointers | O(N) | WithExplaination
Observations\n\nLets take an example to understand solution:\n1. XXXXXXL\nWe can replace \'XL\' with XXXXXLX. Again doing this we can obtain XXXXLXX. Which show
**Observations**\n\nLets take an example to understand solution:\n1. XXXXXXL\nWe can replace \'XL\' with XXXXXLX. Again doing this we can obtain XXXXLXX. Which shows we can move L in left. \nNote: We are able to move L in left as long as there is no R in left side of L.\n\n2. On a similar note we can say R can be moved to right until we have L following R.\n\n**Conclusion**\n\na. L can not cross R and R cannot cross L i.e. Order of LR in original and final string should be same.\nb. R in final string can be at position >= what it was before in original string. It cannot go in left.\nc. L in final string can be at position <= what is was before in original string. It cannot go in right.\n\n```\nclass Solution {\npublic:\n    bool canTransform(string start, string end) {        \n        \n        int len = start.length();\n        int i=0,j=0;\n      \n        while(i<len && j<len){\n            while(start[i]==\'X\'){\n                i++;\n            }\n            while(end[j]==\'X\'){\n                j++;\n            }\n            if(start[i]!=end[j]){\n                return false;\n            }\n            if(start[i]==\'R\' && j<i){\n                return false;\n            }\n            if(start[i]==\'L\' && j>i){\n                return false;\n            }\n            i++;j++;\n        }\n        \n        while(i<len && start[i]==\'X\'){\n            i++;\n        }\n        while(j<len && end[j]==\'X\'){\n            j++;\n        }\n        if(i<len || j<len){\n            return false;\n        }\n        return true;\n    }\n};\n\n```\n\n\nPlease upvote incase you find my explaination useful.
['Two Pointers', 'String']