Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.node
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.google
For example, if we choose the node "gr"
and swap its two children, it produces a scrambled string "rgeat"
.spa
rgeat / \ rg eat / \ / \ r g e at / \ a t
We say that "rgeat"
is a scrambled string of "great"
..net
Similarly, if we continue to swap the children of nodes "eat"
and "at"
, it produces a scrambled string "rgtae"
.code
rgtae / \ rg tae / \ / \ r g ta e / \ t a
We say that "rgtae"
is a scrambled string of "great"
.blog
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.分析:
這個問題是google的面試題。因爲一個字符串有不少種二叉表示法,貌似很難判斷兩個字符串是否能夠作這樣的變換。
對付複雜問題的方法是從簡單的特例來思考,從而找出規律。
先考察簡單狀況:
字符串長度爲1:很明顯,兩個字符串必須徹底相同才能夠。
字符串長度爲2:當s1="ab", s2只有"ab"或者"ba"才能夠。
對於任意長度的字符串,咱們能夠把字符串s1分爲a1,b1兩個部分,s2分爲a2,b2兩個部分,知足((a1~a2) && (b1~b2))或者 ((a1~b2) && (a1~b2))
如此,咱們找到了解決問題的思路。首先咱們嘗試用遞歸來寫。
解法一(遞歸):
兩個字符串的類似的必備條件是含有相同的字符集。簡單的作法是把兩個字符串的字符排序後,而後比較是否相同。
加上這個檢查就能夠大大的減小遞歸次數。
代碼以下:排序
1 class Solution { 2 public: 3 bool isScramble(string s1, string s2) { 4 int l1=s1.length(); 5 int l2=s2.length(); 6 if(l1!=l2) return false; 7 if(l1==1) return s1==s2; 8 int A[26]={0}; 9 for(int i=0;i<l1;i++) 10 ++A[s1[i]-'a']; 11 for(int i=0;i<l2;i++) 12 --A[s2[i]-'a']; 13 for(int i=0;i<26;i++) 14 if(A[i]!=0) return false; 15 bool res=false; 16 for(int i=1;i<l1&&!res;i++){ 17 res=isScramble(s1.substr(0,i),s2.substr(0,i))&&isScramble(s1.substr(i),s2.substr(i)); 18 if(!res) 19 res=isScramble(s1.substr(0,i),s2.substr(l1-i))&&isScramble(s1.substr(i),s2.substr(0,l1-i)); 20 } 21 return res; 22 } 23 };
解法二(動態規劃):
固然,這道題也能夠用動態規劃Dynamic Programming,根據以往的經驗來講,根字符串有關的題十有八九能夠用DP來作,那麼難點就在於如何找出遞推公式。參見網友Code Ganker的博客,這實際上是一道三維動態規劃的題目,咱們提出維護量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)。代碼以下:遞歸
1 // DP 2 class Solution { 3 public: 4 bool isScramble(string s1, string s2) { 5 if (s1.size() != s2.size()) return false; 6 if (s1 == s2) return true; 7 int n = s1.size(); 8 vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false))); 9 for (int i = 0; i < n; ++i) { 10 for (int j = 0; j < n; ++j) { 11 dp[i][j][1] = s1[i] == s2[j]; 12 } 13 } 14 for (int len = 2; len <= n; ++len) { 15 for (int i = 0; i <= n - len; ++i) { 16 for (int j = 0; j <= n - len; ++j) { 17 for (int k = 1; k < len; ++k) { 18 if ((dp[i][j][k] && dp[i + k][j + k][len - k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k])) { 19 dp[i][j][len] = true; 20 } 21 } 22 } 23 } 24 } 25 return dp[0][0][n]; 26 } 27 };