题目:
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.
链接: http://leetcode.com/problems/scramble-string/
题解:
题目比较长,理解起来也很费力。判断两个string是否互为scramble。卡了很久没有思路,今天早上坐PATH的时候看到一些讲解觉得很不错,下午试了试觉得可以。下面是用DFS + 剪枝。
Time Complexity - O(4n), Space Complexity - O(n)。
public class Solution { public boolean isScramble(String s1, String s2) { if(s1 == null || s2 == null || s1.length() != s2.length()) return false; if(s1.equals(s2)) return true; char[] arr1 = s1.toCharArray(); char[] arr2 = s2.toCharArray(); Arrays.sort(arr1); Arrays.sort(arr2); if(!new String(arr1).equals(new String(arr2))) return false; for(int i = 1; i < s1.length(); i++) { String s11 = s1.substring(0, i); String s12 = s1.substring(i); String s21 = s2.substring(0, i); String s22 = s2.substring(i); if(isScramble(s11, s21) && isScramble(s12, s22)) //left - left , right - right return true; s21 = s2.substring(0, s2.length() - i); s22 = s2.substring(s2.length() - i); if(isScramble(s11, s22) && isScramble(s12, s21)) //left - right, right - left return true; } return false; } }
还有一种做法是三维DP,还要仔细研究一下。
二刷:
还是recursive比较好理解一些,三维dp以后再说了。下面分析一下recursive的几个点:
- 首先判断边界
- 其次,当s1等于s2的时候,我们判断可以返回如true
- 否则我们对排序后的 s1和s2进行一个比较,假如不等,则我们舍去
- 否则我们进入遍历的循环体,注意starting index是从1开始
- 我们设置s11, s12, s21和s22,然后递归判断(s11, s21)以及(s12和s22)这两个pair,假如同时满足scramble,则我们可以返回true
- 否则,我们尝试交换过一次的结果,即重设s21和s22从尾部开始split。然后比较新的(s11, s22)以及(s12和s21)这两个pair,假如同时满足条件则返回true
- 否则返回false
- 复杂度的来说 ,不考虑substring的复杂度话, recursive depth大约是n,branching factor是4,所以时间复杂度是大约是O(n4), 空间复杂度也是O(n4)
Java:
Time Complexity - O(4n), Space Complexity - O(n4)。 这里可能算得还是不对,希望有机会能再算算。
public class Solution { public boolean isScramble(String s1, String s2) { if (s1 == null || s2 == null || s1.length() != s2.length()) { return false; } if (s1.equals(s2)) { return true; } char[] arr1 = s1.toCharArray(); char[] arr2 = s2.toCharArray(); Arrays.sort(arr1); Arrays.sort(arr2); if (!String.valueOf(arr1).equals(String.valueOf(arr2))) { return false; } int len = s1.length(); for (int i = 1; i < len; i++) { String s11 = s1.substring(0, i); String s12 = s1.substring(i); String s21 = s2.substring(0, i); String s22 = s2.substring(i); if (isScramble(s11, s21) && isScramble(s12, s22)) { return true; } s21 = s2.substring(0, len - i); s22 = s2.substring(len - i); if (isScramble(s11, s22) && isScramble(s12, s21)) { return true; } } return false; } }
题外话:
2/9/2016
二刷进度一直比较慢,今天要开始加快速度了,准备开启糙快猛节奏。
Reference:
https://leetcode.com/discuss/46803/accepted-java-solution
https://leetcode.com/discuss/36470/share-my-4ms-c-recursive-solution
https://leetcode.com/discuss/3632/any-better-solution
https://leetcode.com/discuss/2504/can-you-partition-string-index-any-time-producing-scramble
http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ascramble-string%EF%BC%8C%E4%B8%89%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/
http://blog.csdn.net/fightforyourdream/article/details/17707187
http://blog.csdn.net/linhuanmars/article/details/24506703
http://www.cnblogs.com/jianxinzhou/p/4712148.html
https://www.slyar.com/blog/depth-first-search-even-odd-pruning.html