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 ...
随机推荐
- django - transaction
def user_atomic(): User.objects.create(name='purk1', email='pwu1@maxprocessing.com') User.objects.cr ...
- 相同的 birthday
Description Sometimes some mathematical results are hard to believe. One of the common problems is t ...
- Sybase ASE报错:server Error: 8242, Severity: 16, State: 1
昨天上午,同事反映某系统在执行存储过程的过程中报错了,报错的信息异常如下: 05:00000:00009:2014/06/09 15:45:30.34 server Error: 8242, Seve ...
- Windows Phone中的几种集合控件
前言 Windows Phone开发过程中不可避免的就是和集合数据打交道,如果之前做过WP App的开发的话,相信你已经看过了各种集合控件的使用.扩展和自定义.这些个内容在这篇博客里都没有,那么我们今 ...
- Mysql 实例分析连接
表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 ...
- C# Socket连接请求超时机制
作者:RazanPaul 译者:Todd Wei 原文:http://www.codeproject.com/KB/IP/TimeOutSocket.aspx 介绍 您可能注意到了,.Net的Syst ...
- 查看MYSQL数据库中所有用户及拥有权限
查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...
- 指向const的指针和const指针
1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...
- python 笔记总结
python 3.5 面向对象:类:具有同种属性的对象称为类,是个抽象的概念.比如说:汽车.人.狗.神:对象:日常生活中的所有东西都是对象,是类的实例化.比如说:推土车是汽车的实例化:姚明是人的实例 ...
- VBS基础篇 - 堆栈
VBS中的堆栈需要使用System.Collections.Stack '建立堆栈 Dim Stk : Set Stk = CreateObject("System.Collections. ...