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.

解答:
1. Brute Force 递归。
基本的思想就是:在S1上找到一个切割点,左边长度为i, 右边长为len - i。 有2种情况表明它们是IsScramble
(1). S1的左边和S2的左边是IsScramble, S1的右边和S2的右边是IsScramble
(2). S1的左边和S2的右边是IsScramble, S1的右边和S2的左边是IsScramble (实际上是交换了S1的左右子树)

而i的取值可以是1  ~  len-1。 基于这个思想,我们可以写出以下的递归Brute Force 解:

引自stellari对复杂度的解释:

stellari

看了你的不少文章,感觉收获良多!只是有点小问题想请教:按照我的理解,那个递归算法在最差情况下应该是O(3^n),而非O(n^2)。理由是:假设函数运行时间为f(n),那么由于在每次函数调用中都要考虑1~n之间的所有长度,并且正反都要检查,所以有
f(n) = 2[f(1) + f(n-1)] +2[f(2) + f(n-2)] … + 2[f(n/2) + f(n/2+1)]. 易推得f(n+1) = 3(fn), 故f(n) = O(3^n)。当然这是最差情况下的时间复杂度。那么你提到的O(n^2),是否是通过其他数学方法得到的更tight的上限?欢迎探讨!

这一个解是不能通过LeetCode的检查的,复杂度是 3^N

 public static boolean isScramble1(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
} int len1 = s1.length();
int len2 = s2.length(); // the two strings should be the same length.
if (len1 != len2) {
return false;
} return rec(s1, s2);
} // Solution 1: The recursion version.
public static boolean rec1(String s1, String s2) {
int len = s1.length(); // the base case.
if (len == 1) {
return s1.equals(s2);
} // 鍒掑垎2涓瓧绗︿覆
for (int i = 1; i < len; i++) {
// we have two situation;
// the left-left right-right & left-right right-left
if (rec1(s1.substring(0, i), s2.substring(0, i))
&& rec1(s1.substring(i, len), s2.substring(i, len))) {
return true;
} if (rec1(s1.substring(0, i), s2.substring(len - i, len))
&& rec1(s1.substring(i, len), s2.substring(0, len - i))) {
return true;
}
} return false;
}

2. 递归加剪枝
感谢unieagle的提示,我们可以在递归中加适当的剪枝,也就是说在进入递归前,先把2个字符串排序,再比较,如果不相同,则直接退出掉。这样也能有效地减少复杂度,具体多少算不清。但能通过leetcode的检查。

 // Solution 2: The recursion version with sorting.
// 鎺掑簭涔嬪悗鐨勫壀鏋濆彲浠ラ�杩嘗eetCode鐨勬鏌�
public static boolean rec(String s1, String s2) {
int len = s1.length(); // the base case.
if (len == 1) {
return s1.equals(s2);
} // sort to speed up.
char[] s1ch = s1.toCharArray();
Arrays.sort(s1ch);
String s1Sort = new String(s1ch); char[] s2ch = s2.toCharArray();
Arrays.sort(s2ch);
String s2Sort = new String(s2ch); if (!s1Sort.equals(s2Sort)) {
return false;
} // 鍒掑垎2涓瓧绗︿覆
for (int i = 1; i < len; i++) {
// we have two situation;
// the left-left right-right & left-right right-left
if (rec(s1.substring(0, i), s2.substring(0, i))
&& rec(s1.substring(i, len), s2.substring(i, len))) {
return true;
} if (rec(s1.substring(0, i), s2.substring(len - i, len))
&& rec(s1.substring(i, len), s2.substring(0, len - i))) {
return true;
}
} return false;
}

3. 递归加Memory

我们在递归中加上记忆矩阵,也可以减少重复运算,但是我们现在就改一下之前递归的结构以方便加上记忆矩阵,我们用index1记忆S1起始地址,index2记忆S2起始地址,len 表示字符串的长度。这样我们可以用一个三维数组来记录计算过的值,同样可以通过leetcode的检查。这个三维数组一个是N^3的复杂度,在每一个递归中,要从1-len地计算一次所有的子串,所以一共的复杂度是N^4

 // Solution 3: The recursion version with memory.
// 閫氳繃璁板繂鐭╅樀鏉ュ噺灏戣绠楅噺
public static boolean isScramble3(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
} int len1 = s1.length();
int len2 = s2.length(); // the two strings should be the same length.
if (len1 != len2) {
return false;
} int[][][] mem = new int[len1][len1][len1];
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len1; j++) {
for (int k = 0; k < len1; k++) {
// -1 means unseted.
mem[i][j][k] = -1;
}
}
} return recMem(s1, 0, s2, 0, len1, mem);
} // Solution 3: The recursion version with memory.
// 閫氳繃璁板繂鐭╅樀鏉ュ噺灏戣绠楅噺
public static boolean recMem(String s1, int index1, String s2, int index2,
int len, int[][][] mem) {
// the base case.
if (len == 1) {
return s1.charAt(index1) == s2.charAt(index2);
} // LEN: 1 - totalLen-1
int ret = mem[index1][index2][len - 1];
if (ret != -1) {
return ret == 1 ? true : false;
} // 鍒濆鍖栦负false
ret = 0; // 鍒掑垎2涓瓧绗︿覆. i means the length of the left side in S1
for (int i = 1; i < len; i++) {
// we have two situation;
// the left-left right-right & left-right right-left
if (recMem(s1, index1, s2, index2, i, mem)
&& recMem(s1, index1 + i, s2, index2 + i, len - i, mem)) {
ret = 1;
break;
} if (recMem(s1, index1, s2, index2 + len - i, i, mem)
&& recMem(s1, index1 + i, s2, index2, len - i, mem)) {
ret = 1;
break;
}
} mem[index1][index2][len - 1] = ret;
return ret == 1 ? true : false;
}

4. 动态规划。

其实如果写出了3,动态规划也就好写了。

三维动态规划题目:

我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble。
有了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i][j][len]。判断这个是不是满足,其实我们首先是把当前s1[i...i+len-1]字符串劈一刀分成两部分,然后分两种情况:第一种是左边和s2[j...j+len-1]左边部分是不是scramble,以及右边和s2[j...j+len-1]右边部分是不是scramble;第二种情况是左边和s2[j...j+len-1]右边部分是不是scramble,以及右边和s2[j...j+len-1]左边部分是不是scramble。如果以上两种情况有一种成立,说明s1[i...i+len-1]和s2[j...j+len-1]是scramble的。而对于判断这些左右部分是不是scramble我们是有历史信息的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
上面说的是劈一刀的情况,对于s1[i...i+len-1]我们有len-1种劈法,在这些劈法中只要有一种成立,那么两个串就是scramble的。
总结起来递推式是res[i][j][len] = || (res[i][j][k]&&res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k]) 对于所有1<=k
如此总时间复杂度因为是三维动态规划,需要三层循环,加上每一步需要线行时间求解递推式,所以是O(n^4)。虽然已经比较高了,但是至少不是指数量级的,动态规划还是相当有用的,空间复杂度是O(n^3)。代码如下:

注:事实上这里最大的难点,是你怎么安排这三个循环。仔细看一下,计算len对应的解时,要用到一堆len-1的解。所以我们应该len 从0到1地这要子计算(三维啊都没办法通过画图来推导动态规划的递增关系了!)

 /*
* Solution 4: The DP Version.
*/
public static boolean isScramble4(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
} int len1 = s1.length();
int len2 = s2.length(); // the two strings should be the same length.
if (len1 != len2) {
return false;
} /*
* i: The index of string 1. j: The index of string 2. k: The length of
* the two string. 1 ~ len1
*
* D[i][j][k] =
*/
boolean[][][] D = new boolean[len1][len1][len1 + 1];
for (int subLen = 1; subLen <= len1; subLen++) {
for (int i1 = 0; i1 <= len1 - subLen; i1++) {
for (int i2 = 0; i2 <= len1 - subLen; i2++) {
if (subLen == 1) {
D[i1][i2][subLen] = s1.charAt(i1) == s2.charAt(i2);
continue;
} D[i1][i2][subLen] = false;
for (int l = 1; l < subLen; l++) {
if (D[i1][i2][l] && D[i1 + l][i2 + l][subLen - l]
|| D[i1][i2 + subLen - l][l] && D[i1 + l][i2][subLen - l]
) {
D[i1][i2][subLen] = true;
break;
}
}
}
}
} return D[0][0][len1];
} /*
* Solution 4: The DP Version. REDO
*/
public static boolean isScramble(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
} int len = s1.length(); if (s2.length() != len) {
return false;
} boolean[][][] D = new boolean[len][len][len + 1]; // D[i][j][k] = D[i][]
for (int k = 1; k <= len; k++) {
// 注意这里的边界选取。 如果选的不对,就会发生越界的情况.. orz..
// attention: should use "<="
for (int i = 0; i <= len - k; i++) {
for (int j = 0; j <= len - k; j++) {
if (k == 1) {
D[i][j][k] = s1.charAt(i) == s2.charAt(j);
continue;
} D[i][j][k] = false;
for (int l = 1; l <= k - 1; l++) {
if (D[i][j][l] && D[i + l][j + l][k - l]
|| D[i][j + k - l][l] && D[i + l][j][k - l] ) {
D[i][j][k] = true;
break;
}
}
}
}
} return D[0][0][len];
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/9241a5148ba94d79c7dfcb3dbbbd3ad5474bdcf1/dp/IsScramble.java

Leetcode:Scramble String 解题报告的更多相关文章

  1. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

  2. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  3. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  4. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  5. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  7. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  8. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  9. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

随机推荐

  1. C语言字符串

    C语言字符串 一.字符串基础 注意:字符串一定以\0结尾. Printf(“yang\n”); 其中yang为字符串常量,“yang”=‘y’+‘a’+‘n’+‘g’+‘\0’.字符串由很多的字符组成 ...

  2. C语言学习笔记 (002) - C++中引用和指针的区别(转载)

    下面用通俗易懂的话来概述一下: 指针-对于一个类型T,T*就是指向T的指针类型,也即一个T*类型的变量能够保存一个T对象的地址,而类型T是可以加一些限定词的,如const.volatile等等.见下图 ...

  3. 【Oracle】Oracle中复合数据类型

    1,常见的操作数据库的技术有那些 jdbc     使用java 访问数据库的技术    PLSQL  (procedure  过程化sql) 在数据库内部操作数据的技术    proc/c++    ...

  4. 用STS和Maven的方式创建一个JavaWeb项目

    一.创建项目 1.Eclipse中用Maven创建项目,选maven-archetype-webapp,如下图: 创建好项目后,目录如下: 至此,项目已经创建完毕,下边是配置.关键所在!!! 二.项目 ...

  5. 嵌入式Linux下MP4视频录制库MP4V2移植和简单介绍

    **************************************************************************************************** ...

  6. #优化:Nginx防御DDOS和CC攻击

    加载HTTP段 ## # 基础配置 ## keepalive_timeout 10; server_tokens off; types_hash_max_size 2048; ## # 主要配置 ## ...

  7. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  8. Android自己定义view之measure、layout、draw三大流程

    自己定义view之measure.layout.draw三大流程 一个view要显示出来.须要经过測量.布局和绘制这三个过程,本章就这三个流程具体探讨一下.View的三大流程具体分析起来比較复杂,本文 ...

  9. android发送短信代码(短信内容超长处理)

    一条短信只可容纳70个中文,所以当短信长度超过70个中文字符时程序就要特殊处理了. 有两种方式: 1.通过sendTextMessage()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...

  10. 关于thymeleaf+layout布局的使用方式,spring boot 访问页面(静态页面及jsp页面)

    首先建立相同部分的html,这里我命名为layout.html,放在了`templates/layout'文件夹下,这个路径以后是会用到的,以下是我的layout的代码,比较粗糙. 但是应该会更好的帮 ...