{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "2d7f231378534510913c1295c153334a",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2)\n{\n\tif (high1 - low1 != high2 - low2)\n\t{\n\t\treturn false;\n\t}\n\telse if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1))\n\t{\n\t\treturn true;\n\t}\n\telse\n\t{\n\t\tint i, c1[128] = {0}, c2[128] = {0};\n\t\tfor (i = low1; i <= high1; i++)\n\t\t{\n\t\t\tc1[s1[i]]++;\n\t\t}\n\t\tfor (i = low2; i <= high2; i++)\n\t\t{\n\t\t\tc2[s2[i]]++;\n\t\t}\n\t\tif (memcmp(c1, c2, 128 * sizeof(int)))\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint len = high1 - low1 + 1;\n\t\t\tfor (i = 1; i < len; i++)\n\t\t\t{\n\t\t\t\tif (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) &&\n\t\t\t\t\tscramble(s1, low1 + i, high1, s2, low2 + i, high2))\n\t\t\t\t{\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) &&\n\t\t\t\t\tscramble(s1, low1 + i, high1, s2, low2, high2 - i))\n\t\t\t\t{\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t}\n}\nstatic bool isScramble(char *s1, char *s2)\n{\n\treturn scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1);\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test s1 s2\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", isScramble(argv[1], argv[2]) ? \"true\" : \"false\");\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-9c4fc46e655e473c8b1fa86ebaa44589",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470832"
}