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.

Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true

Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false

思路:对付复杂问题的方法是从简单的特例来思考。简单情况:

  1. 如果字符串长度为1,那么必须两个字符串完全相同;
  2. 如果字符串长度为2,例如s1='ab',则s2='ab'或s2='ba'才行
  3. 如果字符串任意长度,那么可以把s1分为a1, b1两部分,s2分为a2,b2两部分。需要满足:((a1=a2)&&(b1=b2)) || ((a1=b2)&&(a2=b1)) =>可用递归
class Solution {
public boolean isScramble(String s1, String s2) {
if(s1.length()==1) return s1.equals(s2); String s11;
String s12;
String s21;
String s22;
for(int i = 1; i <= s1.length(); i++){
s11 = s1.substring(0, i);
s12 = s1.substring(i);
s21 = s2.substring(0,i);
s22 = s2.substring(i);
if(isScramble(s11,s21) && isScramble(s12,s22)) return true;
if(isScramble(s11,s22) && isScramble(s12,s21)) return true;
}
return false;
}
}

Result: Time Limit Exceeded

解决方法:动态规划。三维状态dp[i][j][k],前两维分别表示s1和s2的下标起始位置,k表示子串的长度。dp[i][j][k]=true表示s1(i, i+k-1)和s2(j, j+k-1)是scramble。

结合递归法中的逻辑,dp[i][j][k]=true的条件是s1(i, i+split-1)=s2(j, j+split-1) && s1(i+split, i+k-1) = s2(j+split, j+k-1) 或者 s1(i, i+split-1)=s2(j+k-split, j+k-1) && s1(i+split, i+k-1) = s2(j, j+k-split-1)

所以状态转移方程是:如果dp[i][j][split] = true && dp[i+split][j+split][k-split] = true 或者 dp[i][j+k-split][split]=true && dp[i+split][j][k-split]=true,那么dp[i][j][k]=true

初始状态:当k=1的时候dp[i][j][1]=true的条件是s1(i)=s2(j)

class Solution {
public boolean isScramble(String s1, String s2) {
if(s1.length()== 0 || s1.equals(s2)) return true; boolean dp[][][] = new boolean[s1.length()][s1.length()][s1.length()+1]; //initial state
for(int i = 0; i < s1.length(); i++){
for(int j = 0; j < s2.length(); j++){
dp[i][j][1] = s1.charAt(i)==s2.charAt(j);
}
} //state transfer
for(int k = 2; k <= s1.length(); k++){
for(int i = 0; i+k-1 < s1.length(); i++){
for(int j = 0; j+k-1 < s1.length(); j++){
for(int split = 1; split < k; split++){
//如果dp[i][j][split] = true && dp[i+split][j+split][k-split] = true 或者 dp[i][j+k-split][split]=true && dp[i+split][j][k-split]=true,那么dp[i][j][k]=true
if((dp[i][j][split] && dp[i+split][j+split][k-split]) || (dp[i][j+k-split][split] && dp[i+split][j][k-split])){
dp[i][j][k] = true;
break;
}
}
}
}
}
return dp[0][0][s1.length()];
}
}

87. Scramble String (Java)的更多相关文章

  1. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  4. 87. Scramble String

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  5. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. [LeetCode] 87. Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  8. 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 ...

  9. 【LeetCode】87. Scramble String

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

随机推荐

  1. pandas之to_datetime时区转换

    from datetime import date, datetime, timedelta     import time     import pandas as pd     from pand ...

  2. LC 861. Score After Flipping Matrix

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  3. css中的border-collapse属性如何设置表格边框线?(代码示例)

    css中的border-collapse属性如何设置表格边框线?本篇文章就给大家介绍css中的border-collapse属性是什么? border-collapse属性设置表格边框线的方法.有一定 ...

  4. NAT(地址转换技术)详解

    目录 NAT产生背景ip地址基础知识NAT技术的工作原理和特点静态NAT动态NATNAT重载(经常应用到实际中)NAT技术的优缺点优点缺点NAT穿越技术应用层网关(ALG)ALG的实际应用NAT技术的 ...

  5. matplotlib之条形图

    1.知识点 1.plt.figure(figsize=(20,8),dpi=80)设置图像大小,主要参数为figsize(a,b) 2.plt.barh(range(len(a)),b,height= ...

  6. wmic查询ip

    @echo off for /F "usebackq" %%R in (`wmic PATH Win32_NetworkAdapterConfiguration WHERE &qu ...

  7. JavaScript基础入门09

    目录 JavaScript 基础入门09 Event 自定义右键菜单 获取鼠标按键 获取鼠标坐标 获取键盘按键 页面中位置的获取 浏览器的默认行为 冒泡 什么是冒泡 小练习 JavaScript 基础 ...

  8. 在使用DapperExtensions时遇到"其他信息: ConnectionString 属性尚未初始化。"错误

    今天在使用在使用DapperExtensions时遇到"其他信息: ConnectionString 属性尚未初始化."错误. //return conn.GetList<T ...

  9. PJzhang:crunch,一个很好的字典生成工具

    猫宁!!! 之前收集子域名的时候使用过子域名挖掘机这个windows软件,查看了它所使用的字典,基本上是小写字母数字1-4位的一个合集.   36+36*36+36*36*36+36*36*36*36 ...

  10. 【Python开发】matplotlib绘图不显示问题解决plt.show()

    最近在看<Python数据分析>这本书,而自己写代码一直用的是Pycharm,在练习的时候就碰到了plot()绘图不能显示出来的问题.网上翻了一下找到知乎上一篇回答,试了一下好像不行,而且 ...