{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "e729adf6a18e49d38cff091fec24807b",
  "keywords": "字符串,动态规划",
  "title": "扰乱字符串",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code> ：\n<ol>\n<li>如果字符串的长度为 1 ，算法停止</li>\n<li>如果字符串的长度 &gt; 1 ，执行下述步骤：\n<ul>\n<li>在一个随机下标处将字符串分割成两个非空的子字符串。即，如果已知字符串 <code>s</code> ，则可以将其分成两个子字符串 <code>x</code> 和 <code>y</code>\n，且满足 <code>s = x + y</code> 。</li>\n<li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即，在执行这一步骤之后，<code>s</code> 可能是\n<code>s = x + y</code> 或者 <code>s = y + x</code> 。\n</li>\n<li>在 <code>x</code> 和 <code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>\n</ul>\n</li>\n</ol>",
      "language": "markdown"
    },
    {
      "content": "\n<p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em>\n</em>和&nbsp;<code>s2</code>，判断&nbsp;<code>s2</code><em>&nbsp;</em>是否是&nbsp;<code>s1</code><em>&nbsp;</em>的扰乱字符串。如果是，返回\n<code>true</code> ；否则，返回 <code>false</code> 。\n</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s1 = \"great\", s2 = \"rgeat\"\n<strong>输出：</strong>true\n<strong>解释：</strong>s1 上可能发生的一种情形是：\n\"great\" --&gt; \"gr/eat\" // 在一个随机下标处分割得到两个子字符串\n\"gr/eat\" --&gt; \"gr/eat\" // 随机决定：「保持这两个子字符串的顺序不变」\n\"gr/eat\" --&gt; \"g/r / e/at\" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割\n\"g/r / e/at\" --&gt; \"r/g / e/at\" // 随机决定：第一组「交换两个子字符串」，第二组「保持这两个子字符串的顺序不变」\n\"r/g / e/at\" --&gt; \"r/g / e/ a/t\" // 继续递归执行此算法，将 \"at\" 分割得到 \"a/t\"\n\"r/g / e/ a/t\" --&gt; \"r/g / e/ a/t\" // 随机决定：「保持这两个子字符串的顺序不变」\n算法终止，结果字符串和 s2 相同，都是 \"rgeat\"\n这是一种能够扰乱 s1 得到 s2 的情形，可以认为 s2 是 s1 的扰乱字符串，返回 true\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s1 = \"abcde\", s2 = \"caebd\"\n<strong>输出：</strong>false\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 3：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s1 = \"a\", s2 = \"a\"\n<strong>输出：</strong>true\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>s1.length == s2.length</code></li>\n<li><code>1 &lt;= s1.length &lt;= 30</code></li>\n<li><code>s1</code> 和 <code>s2</code> 由小写英文字母组成</li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef isScramble(self, s1, s2, memo={}):\n\t\tif len(s1) != len(s2) or sorted(s1) != sorted(s2):\n\t\t\treturn False\n\t\tif len(s1) <= len(s2) <= 1:\n\t\t\treturn s1 == s2\n\t\tif s1 == s2:\n\t\t\treturn True\n\t\tif (s1, s2) in memo:\n\t\t\treturn memo[s1, s2]\n\t\tn = len(s1)\n\t\tfor i in range(1, n):\n\t\t\ta = self.isScramble(s1[:i], s2[:i], memo) and self.isScramble(s1[i:], s2[i:], memo)\n\t\t\tif not a:\n\t\t\t\tb = self.isScramble(s1[:i], s2[-i:], memo) and self.isScramble(s1[i:], s2[:-i], memo)\n\t\t\tif a or b:\n\t\t\t\tmemo[s1, s2] = True\n\t\t\t\treturn True\n\t\tmemo[s1, s2] = False\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isScramble(s1 = \"great\", s2 = \"rgeat\"))",
    "language": "python"
  },
  "node_id": "dailycode-cf56d018344b4d6598b49593f567d8ec",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470832"
}