87. Scramble String
题目:
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
87. Scramble String的更多相关文章
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- leetcode@ [87] Scramble String (Dynamic Programming)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【LeetCode】87. Scramble String
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- 87. Scramble String *HARD* 动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 87. Scramble String (String; DP)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
随机推荐
- How to change comment
AX2009 // USR Changed on 2013-07-10 at 12:57:46 by 7519 - Begin // USR Changed on 2013-07-10 at 12:5 ...
- 全部快捷方式图标变成LNK文件怎么办
windowsLNK文件打开方式恢复 相信有些用户曾试过错误地把LNK文件打开方式更改其文件导致系统所有快捷方式都失效vista与Windows7系统还普遍使用时候相信大家会有点惊慌失措要紧下面只要大 ...
- java排序集锦
java实现排序的一些方法,来自:http://www.javaeye.com/topic/548520 package sort; import java.util.Random; /** * 排序 ...
- 【BZOJ 1188】 [HNOI2007]分裂游戏
Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...
- 【转】如何设置Android软键盘的默认不弹出?
在开发Anroid的时候,当你打开一个界面的时候,屏幕的焦点会自动停留在第一个EditText中,Android的软键盘默认会自动弹出,用户第一眼连界面都没有看清楚,软键盘就弹出来了,这就影响到了用户 ...
- Flash设置全屏后,放到网页中显示不正常
stage.displayState = StageDisplayState.FULL_SCREEN;//全屏,注意当设置全屏后,放到网页中显示不正常
- C#快速排序算法基础入门篇
相信算法对于许多开发人员来说都是一大难点,之所以难,就像设计模式一样,许多人在阅读之后,没有很好地理解,也不愿意动手上机操作,只停留在理论的学习上面,随着时间推移就慢慢淡忘. 有些东西,你可以发明创造 ...
- d3d11 effect state and default value tables
Blend state State Default ValueAlphaToCoverage Enable FALSEIndependentBlend Enable FALSERenderTarget ...
- CSS function--功能样式
功能样式,从常用样式方法中抽离,按需使用,使用前请先阅读 CSS规范 中相关条列. /* function */ .f-cb:after,.f-cbli li:after{display:block; ...
- Why am I able to change the contents of const char *ptr?
http://stackoverflow.com/questions/3228664/why-am-i-able-to-change-the-contents-of-const-char-ptr I ...