給定一個字串 s1,將 s1 分成 x 和 y 兩段子字串,你可以決定是否將交換兩段子字串的順序,也就是 s1 = x + y,或是 s1 = y + x。接著再對 x, y 分別進行一樣的操作,直到字串都變成長度 1 為止。
現在給你另一個字串 s2,問是否 s1 可以透過上述的操作變成 s2。
範例說明
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12
Input: s1 = "great", s2 = "rgeat" Output: true
Explanation: One possible scenario applied on s1 is: "great" --> "gr/eat" // divide at random index. "gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order. "gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at ranom index each of them. "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. "r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t". "r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order. The algorithm stops now and the result string is "rgeat" which is s2. As there is one possible scenario that led s1 to be scrambled to s2, we return true.