[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 ...
随机推荐
- CSS hack 360浏览器 极速模式与兼容模式
自动切换浏览器模式对于360浏览器7.1版本有效,8.1无效 <%@ Page Language="C#" AutoEventWireup="true" ...
- H3C交换机限制子网之间的相互访问
acl number 3000 rule 1 permit ip source 10.0.5.0 0.0.0.255 destination 172.16.1.100 0 #允许10.0. ...
- jQuery 点击后退(返回)执行函数
<html> <head> <meta charset="UTF-8"> <meta name="viewport" ...
- 以Windows服务方式运行ASP.NET Core程序【转载】
我们对ASP.NET Core的使用已经进行了相当一段时间了,大多数时候,我们的Web程序都是发布到Linux主机上的,当然了,偶尔也有需求要发布到Windows主机上,这样问题就来了,难道直接以控制 ...
- Day 05 可变不可变、数据类型内置方法
1.可变类型:值改变,但是id不变,证明就是改变原值,是可变类型 2.不可变类型:值改变,但是id也跟着改变,证明是产生新的值,是不可变类型 数字类型 一.整型int 1.用途:记录年龄.等级.数量 ...
- ADS1.2和JlinkV8 erro starting external process,Process error code 87(0x57)参数错误
ADS1.2和JlinkV8 erro starting external process,Process error code 87(0x57)参数错误 网上的大致说法是说这个跟W7有关 说是将I ...
- 记一次sql server 2005访问http接口,并解析json的过程
记一次sql server 2005访问http接口,并解析json的过程 JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...
- Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机
1.今天在做数据库升级的时候,遇到一个问题,就是onCreate方法和onUpgrade方法的执行时机的问题,这个当时在操作的时候,没有弄清楚,很是迷糊,后来看了相关的博客由于转发受限所以copy了一 ...
- vue slot
一直觉得vue的slot比较申请,而且比较深奥,总有点不想用的感觉,事实上,在一定程度上,也真的可以完全避开slot就能把一个项目完全搭建完成. 但是随着用的次数越来越多,看到的内容也越来越多的情况, ...
- leetcode240
public class Solution { public bool SearchMatrix(int[,] matrix, int target) { , j = matrix.GetLength ...