Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great":
great/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that "rgeat" is a scrambled string of "great".
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a
We say that "rgtae" is a scrambled string of "great".
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
分析:
這是一道三維動態規劃的題目,我們提出維護量res[i][j][n],其中i是s1的起始字符編號,j是s2的起始字符編號,
而n是當前的字符串長度;res[i][j][len]表示的是:以i和j分別爲s1和s2起點的長度爲len的字符串是不是互爲scramble。
有了維護量我們接下來看看遞推式,也就是怎麼根據歷史信息來得到res[i][j][len]。
判斷這個是不是滿足,其實我們首先是把當前s1[i,……,i+len-1]字符串劈一刀分成兩部分,然後分兩種情況:
第一種情況是:左邊和s2[j,……,j+len-1]左邊部分是不是scramble,以及右邊和s2[j...j+len-1]右邊部分是不是scramble;
第二種情況是:左邊和s2[j...j+len-1]右邊部分是不是scramble,以及右邊和s2[j...j+len-1]左邊部分是不是scramble。
以上兩種情況只要有一種成立,就說明s1[i,……,i+len-1]和s2[j,……,j+len-1]是scramble的。
對於判斷這些左右部分是不是scramble,我們是有歷史信息的,
因爲長度小於n的所有情況我們都在前面求解過了(也就是長度是最外層循環)。
上面說的是劈一刀的情況,對於s1[i,……,i+len-1]我們有len-1種劈法,
在這些劈法中只要有一種成立,那麼兩個串就是scramble的。
總結起來遞推式如下:
res[i][j][len] ||=((res[i][j][k]&&res[i+k][j+k][len-k])||(res[i][j+len-k][k]&&res[i+k][j][len-k]))
對於所有1<=k<len,也就是對於所有len-1種劈法的結果求 或運算。
因爲信息都是計算過的,對於每種劈法只需要常量操作即可完成,因此求解遞推式是需要O(len)(因爲len-1種劈法)。
總時間複雜度:因爲是三維動態規劃,需要三層循環,加上每一步需要線行時間求解遞推式,所以是O(n^4)。
雖然已經比較高了,但是至少不是指數量級的,動態規劃還是有很大優勢的,空間複雜度是O(n^3)。
c++代碼:
class Solution {
public:
bool isScramble(string s1, string s2) {
if (s1.length()!=s2.length()) return false;
if (s1.length()==0) return true;
vector<vector<vector<bool>>> res(s1.size(),vector<vector<bool>>(s2.size(),vector<bool>(s1.size()+1,false)));
//res[i][j][len]表示的是:以i爲s1的起點,同時以j爲s2的起點,二者均取長度爲len的子串,看看這2個子串是否互爲Scramble String
for(int i=0;i<s1.size();i++)
{
for(int j=0;j<s2.size();j++)
{
res[i][j][1]=(s1[i]==s2[j]);//計算len==1時,數組res的取值
}
}
for(int len=2;len<=s1.size();len++)//計算len>1時,數組res的取值
for(int i=0;i<s1.size()-len+1;i++)
for(int j=0;j<s2.size()-len+1;j++)
for(int k=1;k<len;k++)
{
res[i][j][len]=((res[i][j][len])||(res[i][j][k]&&res[i+k][j+k][len-k])||(res[i][j+len-k][k]&&res[i+k][j][len-k]));
}
return res[0][0][s1.length()];
}
};
上述代碼還可進行一點優化:觀察最內層的for循環;初始時res[i][j][len]爲false,當res[i][j][len]變爲true時,說明:以i爲s1的起點,同時以j爲s2的起點,
二者均取長度爲len的子串,這兩個子串滿足Scramble String。此時,可通過break語句跳出最內層循環!!!
優化後的C++代碼如下:
class Solution {
public:
bool isScramble(string s1, string s2) {
if (s1.length()!=s2.length()) return false;
if (s1.length()==0) return true;
vector<vector<vector<bool>>> res(s1.size(),vector<vector<bool>>(s2.size(),vector<bool>(s1.size()+1,false)));
//res[i][j][len]表示的是:以i爲s1的起點,同時以j爲s2的起點,二者均取長度爲len的子串,看看這2個子串是否互爲Scramble String
for(int i=0;i<s1.size();i++)
{
for(int j=0;j<s2.size();j++)
{
res[i][j][1]=(s1[i]==s2[j]);//計算len==1時,數組res的取值
}
}
for(int len=2;len<=s1.size();len++)//計算len>1時,數組res的取值
for(int i=0;i<s1.size()-len+1;i++)
for(int j=0;j<s2.size()-len+1;j++)
for(int k=1;k<len;k++)
{
if(res[i][j][len]==true) break;//跳出最內層for循環
else res[i][j][len]=((res[i][j][k]&&res[i+k][j+k][len-k])||(res[i][j+len-k][k]&&res[i+k][j][len-k]));
}
return res[0][0][s1.length()];
}
};
python代碼如下:
class Solution(object):
def isScramble(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if len(s1)!=len(s2):return False
if len(s1)==0:return True
lens=len(s1)
res=[ [ [False]*(lens+1) for j in range(lens)] for i in range(lens)]#初值全部設爲False
#res[i][j][len]表示的是:以i爲s1的起點,同時以j爲s2的起點,二者均取長度爲len的子串,看看這2個子串是否互爲Scramble String
for i in range(lens):
for j in range(lens):
res[i][j][1]=(s1[i]==s2[j])#計算len==1時,數組res的取值
for length in range(2,lens+1):#計算len>1時,數組res的取值
for i in range(0,lens-length+1):
for j in range(0,lens-length+1):
for k in range(1,length):
res[i][j][length]=((res[i][j][length])or(res[i][j][k] and res[i+k][j+k][length-k])or(res[i][j+length-k][k] and res[i+k][j][length-k]))
return res[0][0][lens]
同理,將上述代碼中的最內層for循環 優化之後,代碼如下:
class Solution(object):
def isScramble(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if len(s1)!=len(s2):return False
if len(s1)==0:return True
lens=len(s1)
res=[ [ [False]*(lens+1) for j in range(lens)] for i in range(lens)]#初值全部設爲False
#res[i][j][len]表示的是:以i爲s1的起點,同時以j爲s2的起點,二者均取長度爲len的子串,看看這2個子串是否互爲Scramble String
for i in range(lens):
for j in range(lens):
res[i][j][1]=(s1[i]==s2[j])#計算len==1時,數組res的取值
for length in range(2,lens+1):#計算len>1時,數組res的取值
for i in range(0,lens-length+1):
for j in range(0,lens-length+1):
for k in range(1,length):
if res[i][j][length]==True:
break #跳出最內層for循環
res[i][j][length]=((res[i][j][k] and res[i+k][j+k][length-k])or(res[i][j+length-k][k] and res[i+k][j][length-k]))
return res[0][0][lens]
(完)