<p>We can scramble a string s to get a string t using the following algorithm:</p>

<ol>
	<li>If the length of the string is 1, stop.</li>
	<li>If the length of the string is &gt; 1, do the following:
	<ul>
		<li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, divide it to <code>x</code> and <code>y</code> where <code>s = x + y</code>.</li>
		<li><strong>Randomly</strong>&nbsp;decide to swap the two substrings or to keep them in the same order. i.e., after this step, <code>s</code> may become <code>s = x + y</code> or <code>s = y + x</code>.</li>
		<li>Apply step 1 recursively on each of the two substrings <code>x</code> and <code>y</code>.</li>
	</ul>
	</li>
</ol>

<p>Given two strings <code>s1</code> and <code>s2</code> of <strong>the same length</strong>, return <code>true</code> if <code>s2</code> is a scrambled string of <code>s1</code>, otherwise, return <code>false</code>.</p>

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

<pre>
<strong>Input:</strong> s1 = &quot;great&quot;, s2 = &quot;rgeat&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> One possible scenario applied on s1 is:
&quot;great&quot; --&gt; &quot;gr/eat&quot; // divide at random index.
&quot;gr/eat&quot; --&gt; &quot;gr/eat&quot; // random decision is not to swap the two substrings and keep them in order.
&quot;gr/eat&quot; --&gt; &quot;g/r / e/at&quot; // apply the same algorithm recursively on both substrings. divide at random index each of them.
&quot;g/r / e/at&quot; --&gt; &quot;r/g / e/at&quot; // random decision was to swap the first substring and to keep the second substring in the same order.
&quot;r/g / e/at&quot; --&gt; &quot;r/g / e/ a/t&quot; // again apply the algorithm recursively, divide &quot;at&quot; to &quot;a/t&quot;.
&quot;r/g / e/ a/t&quot; --&gt; &quot;r/g / e/ a/t&quot; // random decision is to keep both substrings in the same order.
The algorithm stops now, and the result string is &quot;rgeat&quot; which is s2.
As one possible scenario led s1 to be scrambled to s2, we return true.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;abcde&quot;, s2 = &quot;caebd&quot;
<strong>Output:</strong> false
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;a&quot;, s2 = &quot;a&quot;
<strong>Output:</strong> true
</pre>

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

<ul>
	<li><code>s1.length == s2.length</code></li>
	<li><code>1 &lt;= s1.length &lt;= 30</code></li>
	<li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    int func(unordered_map<string,int> &mp, string &s1, string &s2, int x1, int y1, int x2, int y2)
    {
        string sx1 = s1.substr(x1,y1-x1+1), sx2 = s2.substr(x2,y2-x2+1);

        string s= sx1;
        s.push_back(','); 
        s += sx2;
        if(sx1==sx2)
           return mp[s]=1;
        
        if(mp.count(s))
           return mp[s];
        int i = 0, j = y1-x1;
        for(int k = i; k<j; k++)
        {
            int x = func(mp, s1, s2,x1,x1+k,x2,x2+k), y = func(mp, s1, s2,y1-k,y1,x2,x2+k);
            if(x==1)
            {
                x += func(mp, s1, s2,x1+k+1,y1,x2+k+1,y2); 
                if(x==2)
                    return mp[s]=1;
            }
            if(y==1)
            {
                y += func(mp, s1, s2,x1,x1+j-k-1,x2+k+1,y2);
                if(y==2)
                    return mp[s]=1;
            }
        }
        return mp[s]=0;
    }
    bool isScramble(string s1, string s2) {
        int n = s1.size();
        unordered_map<string,int> mp;
        // for(int i = 0; i<n; i++)
        // {
        //     for(int j = i+1; j<n; j++)
        //     {
        //         for(int k = i; k<j; j++)
        //         {
        //             string s = s1.substr(i,k-i+1) + "," + s2.substr(i,k-i+1);
        //             string sx = s1.substr(i,j-k) + "," + s2.substr(k+1,j-k);
        //             if(mp.count(s))
        //             {

        //             }
        //         }
        //     }
        // }
        int ans = func(mp, s1, s2,0,n-1,0,n-1);
        return ans;

    }
};
44933