Algorithm
Problem Name: 87. Scramble String
We can scramble a string s to get a string t using the following algorithm:
- If the length of the string is 1, stop.
- If the length of the string is > 1, do the following:
- Split the string into two non-empty substrings at a random index, i.e., if the string is
s
, divide it tox
andy
wheres = x + y
. - Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step,
s
may becomes = x + y
ors = y + x
. - Apply step 1 recursively on each of the two substrings
x
andy
.
- Split the string into two non-empty substrings at a random index, i.e., if the string is
Given two strings s1
and s2
of the same length, return true
if s2
is a scrambled string of s1
, otherwise, return false
.
Example 1:
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 random 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 one possible scenario led s1 to be scrambled to s2, we return true.
Example 2:
Input: s1 = "abcde", s2 = "caebd" Output: false
Example 3:
Input: s1 = "a", s2 = "a" Output: true
Constraints:
s1.length == s2.length
1 <= s1.length <= 30
s1
ands2
consist of lowercase English letters.
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const isScramble = function(s1, s2) {
if (s1 === s2) return true
const letters = new Array(128).fill(0)
const a = 'a'.charCodeAt(0)
for (let i = 0; i < s1.length; i++) {
letters[s1.charCodeAt(i) - a]++
letters[s2.charCodeAt(i) - a]--
}
for (let i = 0; i < 128; i++) if (letters[i] !== 0) return false
for (let i = 1; i < s1.length; i++) {
if (
isScramble(s1.substring(0, i), s2.substring(0, i)) &&
isScramble(s1.substring(i), s2.substring(i))
)
return true
if (
isScramble(s1.substring(0, i), s2.substring(s2.length - i)) &&
isScramble(s1.substring(i), s2.substring(0, s2.length - i))
)
return true
}
return false
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
class Solution:
def isScramble(self, s1, s2):
n, m = len(s1), len(s2)
if n != m or sorted(s1) != sorted(s2):
return False
if n < 4 or s1 == s2:
return True
f = self.isScramble
for i in range(1, n):
if f(s1[:i], s2[:i]) and f(s1[i:], s2[i:]) or \u005C
f(s1[:i], s2[-i:]) and f(s1[i:], s2[:-i]):
return True
return False
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with C# Programming
Code -
C# Programming
using System.Linq;
namespace LeetCode
{
public class _087_ScrambleString
{
public bool IsScramble(string s1, string s2)
{
if (s1 == s2) { return true; }
var list = Enumerable.Repeat(0, 256).ToArray(); ;
var len = s1.Length;
for (var i = 0; i < len; i++)
{
list[s1[i]]++;
list[s2[i]]--;
}
if (list.Any(i => i > 0)) { return false; }
if (len < = 3) { return true; } // fast pruning.
for (var i = 1; i < len; i++)
{
if ((IsScramble(s1.Substring(0, i), s2.Substring(0, i)) && IsScramble(s1.Substring(i), s2.Substring(i))) ||
(IsScramble(s1.Substring(0, i), s2.Substring(len - i)) && IsScramble(s1.Substring(i), s2.Substring(0, len - i))))
{
return true;
}
}
return false;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output