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 多实例给root用户创建密码
DB:5.5.14 OS:CentOS 6.3 安装多实例MySQL数据库,安装完成后默认无密码,一次性给所有实例的root账户创建密码: #!/bin/bash for i in {3361..3 ...
- Redis 二:入门基本篇
.多数据库设置 - ,代表16个数据库 .glob风格通配符 keys * 返回所有 keys ba? 返回 bar keys b[a-z][a-z] 返回btt bar \x 匹配转义字符 .判断一 ...
- Unable to create the store directory. (Exception from HRESULT: 0x80131468)
一个ASP.NET的程序,使用了MS ReportViewer报告控件,在用该控件导出生成Excel文件时,先是提示行不能超过65535. 这个是由于Excel2003的行限制的原因.由于修改成用Ex ...
- CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie
1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- 第一个Nodejs程序
我的第一个Nodejs程序:Hello World var http = require("http"); http.createServer(function(request, ...
- js 正则 数值验证
function checkTextDataForNORMAL(strValue) { // 特殊字符验证格式 var regTextChar = /([\*"\'<>\/])+ ...
- 关于拓扑排序(topologicalsort)
假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...
- Problem 1007 幸运数 线段树成段更新
题目链接: 题目 Problem 1007 幸运数 Time Limit: 2000 mSec Memory Limit : 131072 KB 问题描述 皮特的幸运数是2和5.只由幸运数字2和5组成 ...
- Ioc 比较
public interface IC { } public class A { IC ic_; public A(IC ic) { ic_ = ic; } } public class B : IC ...