Welcome to Subscribe On Youtube
Question
Formatted question description: https://leetcode.ca/all/87.html
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.
Algorithm
DFS
To determine if two strings s1
and s2
are scramble, there must exist a length l1
in s1
such that it can be divided into s11
and s12
. Similarly, there must exist s21
and s22
such that either s11
and s21
are scramble and s12
and s22
are scramble, or s11
and s22
are scramble and s12
and s21
are scramble.
Consider the example of “rgeat” and “great”. “rgeat” can be divided into “rg” and “eat”, and “great” can be divided into “gr” and “eat”. Since “rg” and “gr” are scrambled and “eat” and “eat” are identical, “rgeat” and “great” are scrambled strings.
DP
To use dynamic programming for this problem, create a three-dimensional array dp[i][j][n]
, where i
represents the starting character of s1
, j
represents the starting character of s2
, and n
represents the current string length. dp[i][j][len]
indicates whether the substring of s1
and s2
with length len
starting from i
and j
, respectively, are scrambles of each other.
To determine dp[i][j][len]
, consider the historical information available. Any string with length less than n
has already been determined. Divide the current s1[i...i+len-1]
string into two parts, and then evaluate the following two cases:
- The first case is where the left part of
s1[i...i+len-1]
is a scramble ofs2[j...j+len-1]
and the right part ofs1[i...i+len-1]
is a scramble ofs2[j...j+len-1]
. - The second case is where the left part of
s1[i...i+len-1]
is a scramble ofs2[j+len-k...j+len-1]
and the right part ofs1[i...i+len-1]
is a scramble ofs2[j...j+len-k-1]
.
If either of the two cases is true, then s1[i...i+len-1]
and s2[j...j+len-1]
are scramble.
There are len-1
possible splitting methods for s1[i...i+len-1]
. If any of these splitting methods yield a true value for the state transition equation, then the two strings are scramble. The state transition equation is:
dp[i][j][len] = || (dp[i][j][k] && dp[i+k][j+k][len-k] || dp[i][j+len-k][k] && dp[i+k][j][len-k])
Code
-
public class Scramble_String { public class Solution { public boolean isScramble(String s1, String s2) { if (s1 == null || s2 == null || s1.length() != s2.length()) { return false; } int len = s1.length(); // dp[i][j], i,j not index, substring i to j of s1/s2 are scrambled 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 n = 1; n < len + 1; n++) { for (int i = 0; i + n - 1 < len; i++) { // @note: here boundary check, I missed for (int j = 0; j + n - 1 < len; j++) { for (int sublen = 1; sublen < n; sublen++) { // same strings: s1="abcde", s2="abcde" boolean ifSame = dp[i][j][sublen] && dp[i + sublen][j + sublen][n - sublen]; // swapped strings: s1="abcde", s2="cdeab" // @note: especially for the index here, using an example for assistance boolean ifSwapped = dp[i][j + n - sublen][sublen] && dp[i + sublen][j][n - sublen]; // dp[i][j][n] = ifSame || ifSwapped; // @note: stupid...being overwritten if (ifSame || ifSwapped) { dp[i][j][n] = true; break; } } } } } return dp[0][0][len]; } } public class Solution_over_time { // clearly a lot of repeated calculation during recursion public boolean isScramble(String s1, String s2) { if (s1 == null || s2 == null || s1.length() != s2.length()) { return false; } if (s1.length() == 0) { return s2.length() == 0; } if (s1.length() == 1) { return s1.charAt(0) == s2.charAt(0); } // basically swap happens for a node of 2 children if (s1.length() == 2) { boolean isSame = s1.equals(s2); boolean isSwapped = s1.charAt(0) == s2.charAt(1) && s1.charAt(1) == s2.charAt(0); return isSame || isSwapped; } for (int i = 1; i < s1.length(); i++) { // @note: left and right child chould NOT be empty // int splitIndex = i; boolean isThisScramble = isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i)); if (isThisScramble) { return true; } } return false; } } } ############ class Solution { public boolean isScramble(String s1, String s2) { int n = s1.length(); boolean[][][] dp = new boolean[n][n][n + 1]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dp[i][j][1] = s1.charAt(i) == s2.charAt(j); } } for (int len = 2; len <= n; ++len) { for (int i1 = 0; i1 <= n - len; ++i1) { for (int i2 = 0; i2 <= n - len; ++i2) { for (int i = 1; i < len; ++i) { if (dp[i1][i2][i] && dp[i1 + i][i2 + i][len - i]) { dp[i1][i2][len] = true; break; } if (dp[i1][i2 + len - i][i] && dp[i1 + i][i2][len - i]) { dp[i1][i2][len] = true; break; } } } } } return dp[0][0][n]; } }
-
// OJ: https://leetcode.com/problems/scramble-string // Time: O(N^(N+1)) // Space: O(N^(N+1)) class Solution { public: bool isScramble(string s1, string s2) { if (s1.size() != s2.size()) return false; if (s1 == s2) return true; for (int i = 1; i < s1.size(); ++i) { string l1 = s1.substr(0, i), r1 = s1.substr(i); string l2 = s2.substr(0, i), r2 = s2.substr(i); string r2r = s2.substr(0, s1.size() - i), l2r = s2.substr(s1.size() - i); if ((isScramble(l1, l2) && isScramble(r1, r2)) || (isScramble(l1, l2r) && isScramble(r1, r2r))) return true; } return false; } };
-
class Solution: def isScramble(self, s1: str, s2: str) -> bool: n = len(s1) dp = [[[False] * (n + 1) for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(n): dp[i][j][1] = s1[i] == s2[j] for l in range(2, n + 1): for i1 in range(n - l + 1): for i2 in range(n - l + 1): for i in range(1, l): if dp[i1][i2][i] and dp[i1 + i][i2 + i][l - i]: dp[i1][i2][l] = True break if dp[i1][i2 + l - i][i] and dp[i1 + i][i2][l - i]: dp[i1][i2][l] = True break return dp[0][0][n] ############ class Solution(object): def isScramble(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ n = len(s1) m = len(s2) if sorted(s1) != sorted(s2): return False if n < 4 or s1 == s2: return True for i in range(1, n): if self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:]): return True if self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:], s2[:-i]): return True return False
-
public class Solution { public bool IsScramble(string s1, string s2) { if (s1.Length != s2.Length) return false; var length = s1.Length; if (length == 0) return true; var f = new bool[length + 1, length, length]; for (var i = 0; i < length; ++i) { for (var j = 0; j < length; ++j) { f[1, i, j] = s1[i] == s2[j]; } } for (var i = 2; i <= length; ++i) { for (var j = 0; j <= length - i; ++j) { for (var k = 0; k <= length - i; ++k) { for (var l = 1; l < i; ++l) { if (f[l, j, k] && f[i - l, j + l, k + l] || f[l, j, k + i - l] && f[i - l, j + l, k]) { f[i, j, k] = true; break; } } } } } return f[length, 0, 0]; } }
-
func isScramble(s1 string, s2 string) bool { n := len(s1) dp := make([][][]bool, n+1) for i := range dp { dp[i] = make([][]bool, n) for j := range dp[i] { dp[i][j] = make([]bool, n+1) } } for i := 0; i < n; i++ { for j := 0; j < n; j++ { dp[i][j][1] = s1[i] == s2[j] } } for l := 2; l < n+1; l++ { for i1 := 0; i1 < n-l+1; i1++ { for i2 := 0; i2 < n-l+1; i2++ { for i := 1; i < l; i++ { if dp[i1][i2][i] && dp[i1+i][i2+i][l-i] { dp[i1][i2][l] = true break } if dp[i1][i2+l-i][i] && dp[i1+i][i2][l-i] { dp[i1][i2][l] = true break } } } } } return dp[0][0][n] }
-
function isScramble(s1: string, s2: string): boolean { const n = s1.length; const f = new Array(n) .fill(0) .map(() => new Array(n).fill(0).map(() => new Array(n + 1).fill(-1))); const dfs = (i: number, j: number, k: number): boolean => { if (f[i][j][k] !== -1) { return f[i][j][k] === 1; } if (k === 1) { return s1[i] === s2[j]; } for (let h = 1; h < k; ++h) { if (dfs(i, j, h) && dfs(i + h, j + h, k - h)) { return Boolean((f[i][j][k] = 1)); } if (dfs(i + h, j, k - h) && dfs(i, j + k - h, h)) { return Boolean((f[i][j][k] = 1)); } } return Boolean((f[i][j][k] = 0)); }; return dfs(0, 0, n); }