leetcode--Scramble String
来源:互联网 发布:淘宝如何设置价格区间 编辑:程序博客网 时间:2024/06/05 19:39
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.
题意:如图,判断两个字符串是否是scrambled关系。
分类:动态规划,字符串
解法1:三维动态规划。简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。
那么要么s11和s21是scramble的并且s12和s22是scramble的(条件1);
要么s11和s22是scramble的并且s12和s21是scramble的(条件2)。
思路是建立一个boolean flag[i][j][len],表明s1从i开始,s2从j开始,len长度的字符串,两者是不是scrambled关系。所以我们求的是flag[0][0][len]
根据上面的说法,要判断flag[i][j][len]是否为true,我们有两个判断依据
首先,我们尝试将s1分成两部分,因为起始序号是i,结束序号是i+len-1
假设我们从i开始,k长度把s1分成s11,s12,那么0<=k<=len
所以我们要判断res[i][j][k]&&res[i+k][j+k][len-k]是否为true,如果是,则满足条件1,
另外,我们还可以根据条件2
判断res[i][j+len-k][k]&&res[i+k][j][len-k]是否为true
原文链接http://blog.csdn.net/crazy__chen/article/details/47377069
- LeetCode : Scramble String
- [LeetCode] Scramble String
- [LeetCode] Scramble String
- [Leetcode] Scramble String
- [leetcode] Scramble String
- LeetCode: Scramble String
- [LeetCode]Scramble String
- [leetcode]Scramble String
- Leetcode:Scramble String
- LeetCode-Scramble String
- LeetCode Scramble String
- [leetcode] Scramble String
- [LeetCode] Scramble String
- LeetCode - Scramble String
- [LeetCode]Scramble String
- [LeetCode] Scramble String
- Leetcode: Scramble String
- leetcode Scramble String
- 兼容Android7.0自动安装 FileProvider 解决下载完成后无法自动安装
- python面向对象
- Redis能干啥?细看11种Web应用场景
- $.ajax跨域实例
- ZendStudio
- leetcode--Scramble String
- 微信登录接口
- FAGLFLEXA 表和 BSEG 表有什么区别
- 一文带你读懂DeepMind新论文,关联推理为什么是智能最重要的特征
- Oracle和SQL语句的优化策略(基础篇)
- Android全面屏适配
- MySQL金融版发布会:4个男人能擦出的火花
- java基础之多线程的练习题
- JAVA虚拟机学习