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

题意:

对于一个字符串,若通过一些列树形颠倒(确定一点后,前后子串交换)得到一个新的字符串,则互为Scramble String

思路:

这道题面试很低频,因为若要dp解,则需三维dp,难度很高不适合面试

直接用递归,观察规律:

若s1 和 s2 长度都为1 : 两字符串必须完全相当

若s1 和 s2 长度都为2: 则当s1 = "ab"时, s2 = "ab" || "ba"

若s1 和 s2 长度都为3: 将 s1 分成 s1Left 和 s1Right两部分

s2 分成 s2Left 和 s2Right 两部分

则 ( s1Left  isScrambled s2Left  && s1Right isScrambled s2Right ) 或者  (s1Left  isScrambled s2Right && s1Right isScrambled s2Left  )

如图,

所以,

先判断s1和s2是否是valid anagram (变位词): 意味着两个字符串长度一致,互相只是各个字符串变换了位置

再递归生成s1的左边、右边,s2 的左边、右边

递归出口: s1等于s2 return true

代码:

 class Solution {
public boolean isScramble(String s1, String s2) {
// corner
if(s1.length() != s2.length() || s1 == null || s2 == null) return false;
// recursion 出口
if(s1.equals(s2)) return true; // 一定要加,否则recursion 没有出口
// valid anagram or not
int[]map = new int[256];
for(int i = 0; i < s1.length(); i++){
map[s1.charAt(i)] ++;
map[s2.charAt(i)] --;
}
for(int i : map){
if( i != 0){
return false;
}
}
// recursion
for(int i = 1; i < s1.length(); i++){
if(isScramble(s1.substring(0,i), s2.substring(0,i)) && isScramble(s1.substring(i), s2.substring(i))) return true;
if(isScramble(s1.substring(0,i), s2.substring(s2.length()-i)) && isScramble(s1.substring(i), s2.substring(0, s2.length()-i))) return true; }
return false;
} }

[leetcode]87. Scramble String字符串树形颠倒匹配的更多相关文章

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

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

  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 爬行字符串

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

  4. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

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

  5. Leetcode#87 Scramble String

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

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

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

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

  8. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

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

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

随机推荐

  1. SpringBoot事务注解@Transactional

    SpringBoot提供了非常方便的事务操作,通过注解就可以实现事务的回滚,非常方便快捷,下面我们就说一下如何进行事务操作. 1. 事务说明 在Spring中,事务有两种实现方式,分别是编程式事务管理 ...

  2. VUE 进行微信支付,解决 微信支付URL未注册

    使用history方式 比较坑吧就不吐槽了,说下实现方式 需要解决问题: 1.因为我的微信支付授权路由是:m.xxxx.com,this.$router.push('xxx')之后经常出现 [微信支付 ...

  3. 服务器tcp连接timewait过多优化及详细分析

    [背景说明] 在7层负载均衡上,查询网络状态发现timewait太多,于是开始准备优化事宜 整体的拓扑结构,前面是lvs做dr模式的4层负载均衡,后端使用(nginx.or haproxy)做7层负载 ...

  4. 静态初始化块和main方法哪个先被执行?

    直接看代码 public class BlockAndMain { public static void main(String[] args) { System.out.println(" ...

  5. sqlserver 带输出参数的存储过程的创建与执行

    创建 use StudentManager go if exists(select * from sysobjects where name='usp_ScoreQuery4') drop proce ...

  6. Learn the Basics - RN2

    使用Image 1. 引用 import { Image } from 'react-native'; 2. 使用 format: <Image source={{}} style{{}} /& ...

  7. 回滚的意义---JDBC事务回滚探究

    JDBC手动事务提交回滚的常见写法一直是rollback写在commit的catch之后: try{ conn.setAutoCommit(false); ps.executeUpdate(); ps ...

  8. Java应用常用性能分析工具

    Java应用常用性能分析工具 好的工具有能有效改善和提高工作效率或加速分析问题的进度,笔者将从事Java工作中常用的性能工具和大家分享下,如果感觉有用记得投一票哦,如果你有好的工具也可以分享给我 工具 ...

  9. fastjson的@JSONField注解

    @JSONField作用:在字段和方法上1.Field:@JSONField作用在Field时,name可以定义输入key的名字,反序列化的时 值不会赋值到属性上2.作用在setter和getter方 ...

  10. Selenium IDE录制脚本时弹出窗口的完美处理

    很多朋友录制脚本时新打开弹出窗口后无法定位元素,我也遇到同样的问题,国内没有什么好的资料,于是就阅读英文,不断尝试,感觉那个selectWindow(title)什么就是个坑,我用这种方法成功处理后得 ...