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.
Java Solution
To solve this problem, you can use dynamic programming. Here’s a Java solution:
public class ScrambleString {
public static boolean isScramble(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
int len = s1.length();
boolean[][][] dp = new boolean[len][len][len + 1];
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
dp[i][j][1] = s1.charAt(i) == s2.charAt(j);
}
}
for (int length = 2; length <= len; length++) {
for (int i = 0; i <= len - length; i++) {
for (int j = 0; j <= len - length; j++) {
for (int k = 1; k < length; k++) {
if ((dp[i][j][k] && dp[i + k][j + k][length - k]) || (dp[i][j + length - k][k] && dp[i + k][j][length - k])) {
dp[i][j][length] = true;
break;
}
}
}
}
}
return dp[0][0][len];
}
public static void main(String[] args) {
System.out.println(isScramble("great", "rgeat")); // Output: true
System.out.println(isScramble("abcde", "caebd")); // Output: false
System.out.println(isScramble("a", "a")); // Output: true
}
}
This Java code uses a three-dimensional array dp
to store intermediate results. The variable dp[i][j][k]
is true if the substring of length k
starting from index i
in s1
and the substring of length k
starting from index j
in s2
are scrambled strings. The algorithm iteratively builds up the dynamic programming table based on smaller substrings until it determines if the entire strings are scrambled.
Python Solution
def isScramble(s1, s2):
if len(s1) != len(s2):
return False
length = len(s1)
dp = [[[False] * (length + 1) for _ in range(length)] for _ in range(length)]
for i in range(length):
for j in range(length):
dp[i][j][1] = s1[i] == s2[j]
for l in range(2, length + 1):
for i in range(length - l + 1):
for j in range(length - l + 1):
for k in range(1, l):
if (dp[i][j][k] and dp[i + k][j + k][l - k]) or (dp[i][j + l - k][k] and dp[i + k][j][l - k]):
dp[i][j][l] = True
break
return dp[0][0][length]
# Test cases
print(isScramble("great", "rgeat")) # Output: True
print(isScramble("abcde", "caebd")) # Output: False
print(isScramble("a", "a")) # Output: True
This Python code uses a three-dimensional array dp
to store intermediate results, similar to the Java solution. The variable dp[i][j][k]
is True
if the substring of length k
starting from index i
in s1
and the substring of length k
starting from index j
in s2
are scrambled strings. The algorithm iteratively builds up the dynamic programming table based on smaller substrings until it determines if the entire strings are scrambled.