[leetcode]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
题意:
对于一个字符串,若通过一些列树形颠倒(确定一点后,前后子串交换)得到一个新的字符串,则互为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字符串树形颠倒匹配的更多相关文章
- [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 爬行字符串
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 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode】87. Scramble String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...
随机推荐
- linux kernel driver debug
1. print printk(): never pr_debug(): always good dev_dbg(): prefered when you have a struct device o ...
- Fabric的settings用法
http://fabric-chs.readthedocs.io/zh_CN/chs/api/core/context_managers.html?highlight=with%20settings# ...
- Day 06 元组,字典,集合
元组 一.定义:参数为for可以循环的对象(可迭代对象) t2 = tuple("123")print(t2, type(t2))t3 = tuple([1, 2, 3])prin ...
- C语言数组指针
C语言中的数组指针与指针数组: ·数组指针一.区分 首先我们需要了解什么是数组指针以及什么是指针数组,如下: int *p[5];int (*p)[5];数组指针的意思即为通过指针引用数组,p先和*结 ...
- WebLogic的下载与安装
一.WebLogic的介绍 WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...
- 【java】this用法
this代表当前类的引用对象:哪个对象调用方法,该方法内部的this就代表那个对象this关键字主要有两三个应用: (1)this调用本类中的属性,也就是类中的成员变量: class People { ...
- 彻底卸载Windows 10中OneDrive
批处理来源网上. @rem OneDrive Complete uninstaller batch process for Windows 10. @rem Run as administrator ...
- javascript常用工具类整理(copy)
JavaScript常用工具类 类型 日期 数组 字符串 数字 网络请求 节点 存储 其他 1.类型 isString (o) { //是否字符串 return Object.prototype.to ...
- Oracle 学习笔记(六)
Oracle 数据库常用的闪回sql 语句及其它操作语句: --Oracle 数据库dml sql -- 查看当前用户所拥有的表 select * from tab; --表空间,auto: 自动管理 ...
- 面向的phthon2+3 的场景,Anaconda 安装+环境配置+管理
standard procedure in pyCharm for creating environment when Anaconda installed Create a conda env vi ...