<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>

#include <string>
#include <unordered_map>
#include <vector>

class Solution {
public:
    // Memoization table to store the results of subproblems
        std::unordered_map<std::string, bool> memo;

            // Function to check if s2 is a scrambled version of s1
                bool isScramble(std::string s1, std::string s2) {
                        // Check base cases
                                if (s1 == s2) return true;
                                        if (s1.length() != s2.length()) return false;

                                                // Check if sorted versions of s1 and s2 match
                                                        std::string key = s1 + " " + s2;
                                                                if (memo.find(key) != memo.end()) return memo[key];

                                                                        // Check if sorted strings are equal
                                                                                if (!isAnagram(s1, s2)) {
                                                                                            memo[key] = false;
                                                                                                        return false;
                                                                                                                }

                                                                                                                        // Try splitting the strings into substrings and check both swap and no swap scenarios
                                                                                                                                int n = s1.length();
                                                                                                                                        for (int i = 1; i < n; ++i) {
                                                                                                                                                    // Scenario 1: No swap
                                                                                                                                                                if (isScramble(s1.substr(0, i), s2.substr(0, i)) &&
                                                                                                                                                                                isScramble(s1.substr(i), s2.substr(i))) {
                                                                                                                                                                                                return memo[key] = true;
                                                                                                                                                                                                            }
                                                                                                                                                                                                                        // Scenario 2: Swap
                                                                                                                                                                                                                                    if (isScramble(s1.substr(0, i), s2.substr(n - i)) &&
                                                                                                                                                                                                                                                    isScramble(s1.substr(i), s2.substr(0, n - i))) {
                                                                                                                                                                                                                                                                    return memo[key] = true;
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                return memo[key] = false;
                                                                                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                                                                                        // Helper function to check if two strings are anagrams
                                                                                                                                                                                                                                                                                                            bool isAnagram(const std::string& s1, const std::string& s2) {
                                                                                                                                                                                                                                                                                                                    if (s1.size() != s2.size()) return false;
                                                                                                                                                                                                                                                                                                                            std::vector<int> count(26, 0);
                                                                                                                                                                                                                                                                                                                                    for (char c : s1) count[c - 'a']++;
                                                                                                                                                                                                                                                                                                                                            for (char c : s2) {
                                                                                                                                                                                                                                                                                                                                                        if (--count[c - 'a'] < 0) return false;
                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                        return true;
                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                            };
                                                                                                                                                                                                                                                                                                                                                                            
19730