<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:
    bool canTransform(string a, string b) {
        int cl=0, cr=0;
        vector<int> a1, b1;
        for (int i=0; i!=a.size(); i++)
        {
            if (a[i]=='R')
            {
                if (cl==0) cr++;
                else
                {
                    a1.push_back(-cl);
                    cl=0;
                    cr++;
                }
            }
            else if (a[i]=='L')
            {
                if (cr==0) cl++;
                else
                {
                    a1.push_back(cr);
                    cr=0;
                    cl++;
                }
            }
        }
        if (cr>0) a1.push_back(cr);
        else if (cl>0) a1.push_back(-cl);

        cl=0; cr=0;
        for (int i=0; i!=b.size(); i++)
        {
            if (b[i]=='R')
            {
                if (cl==0) cr++;
                else
                {
                    b1.push_back(-cl);
                    cl=0;
                    cr++;
                }
            }
            else if (b[i]=='L')
            {
                if (cr==0) cl++;
                else
                {
                    b1.push_back(cr);
                    cr=0;
                    cl++;
                }
            }
        }
        if (cr>0) b1.push_back(cr);
        else if (cl>0) b1.push_back(-cl);

        if (a1!=b1) return 0;

        int la=0, lb=0;
        while (la<a.size()&&lb<b.size())
        {
            while (la<a.size()&&a[la]==b[lb]) {la++; lb++;}
            if (la==a.size()) break;
            if (b[lb]=='L')
            {
                if (a[lb]=='R') return 0;
                else
                {
                    while (la<a.size()&&a[la]=='X') la++;
                    if (la==a.size()) return 0;
                    if (a[la]=='R') return 0;
                    if (a[la]=='L')
                    {
                        swap(a[la], a[lb]);
                    }
                }
            }
            else if (a[la]=='L') return 0;
        lb++;
        la=lb;
        }

        la=a.size()-1; lb=la;
        while (la>=0&&lb>=0)
        {
            while (la>=0&&a[la]==b[lb]) {la--; lb--;}
            if (la==-1) break;
            if (b[lb]=='R')
            {
                if (a[lb]=='L') return 0;
                else
                {
                    while (la>=0&&a[la]=='X') la--;
                    if (la==-1) return 0;
                    if (a[la]=='L') return 0;
                    if (a[la]=='R')
                    {
                        swap(a[la], a[lb]);
                    }
                }
            }
            else if (a[la]=='R') return 0;
        lb--;
        la=lb;
        }
        return 1;
    }
};
9600