<p>In a string composed of <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;X&#39;</code> characters, like <code>&quot;RXXLRXRXL&quot;</code>, a move consists of either replacing one occurrence of <code>&quot;XL&quot;</code> with <code>&quot;LX&quot;</code>, or replacing one occurrence of <code>&quot;RX&quot;</code> with <code>&quot;XR&quot;</code>. Given the starting string <code>start</code> and the ending string <code>end</code>, return <code>True</code> if and only if there exists a sequence of moves to transform <code>start</code> to <code>end</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> start = &quot;RXXLRXRXL&quot;, end = &quot;XRLXXRRLX&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can transform start to end following these steps:
RXXLRXRXL -&gt;
XRXLRXRXL -&gt;
XRLXRXRXL -&gt;
XRLXXRRXL -&gt;
XRLXXRRLX
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> start = &quot;X&quot;, end = &quot;L&quot;
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= start.length&nbsp;&lt;= 10<sup>4</sup></code></li>
	<li><code>start.length == end.length</code></li>
	<li>Both <code>start</code> and <code>end</code> will only consist of characters in <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and&nbsp;<code>&#39;X&#39;</code>.</li>
</ul>

class Solution {
public:
    //XL -> LX
    //RX -> XR
    //T.C -> O(N)
    //S.C -> O(1)
    bool canTransform(string start, string end) {
        //Lengths are differnet
        if(start.length() != end.length())
        {
            return false;
        }

        //Check if without X it is matching or not??
        string start_chars, end_chars;

        for(char c: start)
        {
            if(c != 'X') start_chars += c;
        }

        for(char c: end)
        {
            if(c != 'X') end_chars += c;
        }

        //chck without x the strings are matched or not??
        if(start_chars != end_chars)
        {
            return false;
        }

        //Now the math part
        int p1 = 0, p2 = 0;
        int n = start.size();

        while(p1 < n && p2 < n)
        {
            //1. Skip 'X' in both strings
            while(p1 < n && start[p1] == 'X') p1++;
            while(p2 < n && end[p2] == 'X') p2++;

            //2. If both pointer reach the end, the strings are transformable
            if(p1 == n && p2 == n)
            {
                return true;
            }

            //3. If only one pointer reach the end, the strings are not transformable
            if(p1 == n || p2 == n)
            {
                return false;
            }

            //4. If current character do not match transfirmation is not possible
            if(start[p1] != end[p2])
            {
                return false;
            }

            //5.'L' Should not move right -> should be left or same position
            if(start[p1] == 'L' && p1 < p2)
            {
                return false;
            }

            //6. 'R' Should not move left -> should be right or same position
            if(start[p1] == 'R' && p1 > p2)
            {
                return false;
            }

            p1++;
            p2++;
        }

        return true;

    }
};
9100