0087. 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
.
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 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.
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 lower-case English letters.
Using cache + recursion¶
class Solution {
bool DP_helper(unordered_map<string, bool> &isScramblePair, string s1, string s2)
{
int i,len = s1.size();
bool res = false;
if(0==len) return true;
else if(1==len) return s1 == s2;
else
{
if(isScramblePair.count(s1+s2)) return isScramblePair[s1+s2]; // checked before, return intermediate result directly
if(s1==s2) res=true;
else{
for(i=1; i<len && !res; ++i)
{
//check s1[0..i-1] with s2[0..i-1] and s1[i..len-1] and s2[i..len-1]
res = res || (DP_helper(isScramblePair, s1.substr(0,i), s2.substr(0,i))
&& DP_helper(isScramblePair, s1.substr(i,len-i), s2.substr(i,len-i)));
//if no match, then check s1[0..i-1] with s2[len-k.. len-1] and s1[i..len-1] and s2[0..len-i]
res = res || (DP_helper(isScramblePair, s1.substr(0,i), s2.substr(len-i,i))
&& DP_helper(isScramblePair, s1.substr(i,len-i), s2.substr(0,len-i)));
}
}
return isScramblePair[s1+s2]= res; //save the intermediate results
}
}
public:
bool isScramble(string s1, string s2) {
unordered_map<string, bool> isScramblePair;
return DP_helper(isScramblePair, s1, s2);
}
};
A string can be rotated anyway.
There are four things to compare between two strings:
s1: 111i222len
s2: 333i444len
111 && 333
222 && 444
or
111 && 444
222 && 333
DP bottom-up¶
class Solution {
public:
bool isScramble(string s1, string s2) {
int sSize = s1.size(), len, i, j, k;
if(0==sSize) return true;
if(1==sSize) return s1==s2;
bool isS[sSize+1][sSize][sSize];
for(i=0; i<sSize; ++i)
for(j=0; j<sSize; ++j)
isS[1][i][j] = s1[i] == s2[j];
for(len=2; len <=sSize; ++len)
for(i=0; i<=sSize-len; ++i)
for(j=0; j<=sSize-len; ++j)
{
isS[len][i][j] = false;
for(k=1; k<len && !isS[len][i][j]; ++k)
{
isS[len][i][j] = isS[len][i][j] || (isS[k][i][j] && isS[len-k][i+k][j+k]);
isS[len][i][j] = isS[len][i][j] || (isS[k][i+len-k][j] && isS[len-k][i][j+k]);
}
}
return isS[sSize][0][0];
}
};
dp[len][i][j]: s1[i..i+len-1]
is a scramble of s2[j..j+len-1]
- init: all single char can match if they are the same (line 11)
dp[len][i][j] = (dp[k][i][j] && dp[len-k][i+k][j+k]) || (dp[k][i+len-k][j] && dp[len-k][i][j+k])
Time Complexity¶
O(n^4) for all cases
Last update:
April 1, 2022