|
| 1 | +<h2><a href="https://leetcode.com/problems/scramble-string/">87. Scramble String</a></h2><h3>Hard</h3><hr><div><p>We can scramble a string s to get a string t using the following algorithm:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li>If the length of the string is 1, stop.</li> |
| 5 | + <li>If the length of the string is > 1, do the following: |
| 6 | + <ul> |
| 7 | + <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> |
| 8 | + <li><strong>Randomly</strong> 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> |
| 9 | + <li>Apply step 1 recursively on each of the two substrings <code>x</code> and <code>y</code>.</li> |
| 10 | + </ul> |
| 11 | + </li> |
| 12 | +</ol> |
| 13 | + |
| 14 | +<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> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre><strong>Input:</strong> s1 = "great", s2 = "rgeat" |
| 20 | +<strong>Output:</strong> true |
| 21 | +<strong>Explanation:</strong> One possible scenario applied on s1 is: |
| 22 | +"great" --> "gr/eat" // divide at random index. |
| 23 | +"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order. |
| 24 | +"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at random index each of them. |
| 25 | +"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order. |
| 26 | +"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t". |
| 27 | +"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order. |
| 28 | +The algorithm stops now, and the result string is "rgeat" which is s2. |
| 29 | +As one possible scenario led s1 to be scrambled to s2, we return true. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<pre><strong>Input:</strong> s1 = "abcde", s2 = "caebd" |
| 35 | +<strong>Output:</strong> false |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong class="example">Example 3:</strong></p> |
| 39 | + |
| 40 | +<pre><strong>Input:</strong> s1 = "a", s2 = "a" |
| 41 | +<strong>Output:</strong> true |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>s1.length == s2.length</code></li> |
| 49 | + <li><code>1 <= s1.length <= 30</code></li> |
| 50 | + <li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li> |
| 51 | +</ul> |
| 52 | +</div> |
0 commit comments