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 ...
随机推荐
- MySQL数据库主从复制
一.MySQ主从复制(主库写入数据,从库读取数据) MySql官方下载地址:http://dev.mysql.com/downloads/mysql/ MySql常用命令: 设置密码 UPDATE U ...
- 《零成本实现Web自动化测试--基于Selenium》 第四章 Selenium 命令
Selenium 命令,通常被称为Selenese,由一系列运行测试案例所需要的命令构成.按顺序排列这些命令就构成了测试脚本. 一. 验证颜面元素 1.Assertion或者Verification ...
- Python标准库之urllib,urllib2自定义Opener
urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能.要支持这些功能,必须使用build_opener()函数创建自定义Opener对象. 1. build_open ...
- PHP中应用Service Locator服务定位及单例模式
单例模式将一个对象实例化后,放在静态变量中,供程序调用. 服务定位(ServiceLocator)就是对象工场Factory,调用者对象直接调用Service Locator,与被调用对象减轻了依赖关 ...
- 设计模式之单实例模式(Singleton)
原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...
- Java7 新特性 switch 可以使用String
今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...
- c++ 钻石继承
在C++中,什么叫做钻石问题(也可以叫菱形继承问题),怎么避免它? 下面的图表可以用来解释钻石问题. 假设我们有类B和类C,它们都继承了相同的类A.另外我们还有类D,类D通过多重继承机制继承了类B和类 ...
- js String Trim函数
<javascript> String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,"& ...
- hdu 2940
简单的大数乘法,直接改16进制~~ #include <cstdio> #include <cstdlib> #include <cmath> #include & ...
- Android 问题流水总结
先来一篇环境搭建的博客 这些都是大同小异. http://blog.csdn.net/yzhj2005/article/details/6980676 http://blog.csdn.net/wan ...