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
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.
思路:这一天假设理解思想之后去做感觉不是非常难。可是在想到解法之前还是有一定难度。
本题解法为递归解法,动态规划解法没有掌握。
递归的思路和遍历字符串,切割成两部分,对照两部分能否scramble,只是本题要比較前前和前后两次。
详细代码例如以下:
public class Solution {
public boolean isScramble(String s1, String s2) {
/**
* 思想是递归,将字符串逐个的分为两串
* 然后让两串的前前比較、后后比較,是则返回true
* 不是着前后比較。是则返回true
* 假设还不是。则分割的字符串位置+1
*/
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
//调用函数推断
if(isScr(c1, c2, 0, c1.length, 0, c2.length)){
return true;
}
return false;
}
boolean isScr(char[] c1,char[] c2,int start1,int end1,int start2, int end2){
//推断字符是否为空
if(end1 - start1 <= 0 && end2 - start2 <= 0){
return true;
}
//假设长度为1。则必须相等
if(end1 - start1 == 1 && end2 - start2 == 1){
return c1[start1] == c2[start2];
}
//长度不等返回false
if( end1 - start1 != end2 - start2){
return false;
}
int[] a = new int[128];
//每一个字符串的字符必须个数相等
for(int i = 0; i < end1 - start1; i++){
a[c1[i+start1]]++;
a[c2[i+start2]]--;
}
//不相等返回false
for(int i = 0; i < a.length; i++){
if(a[i] != 0){
return false;
}
}
//递归实现
for(int i = 1; i < end1 - start1; i++){
if(isScr(c1, c2, start1, start1+i, start2, start2+i) && isScr(c1, c2, start1+i, end1, start2+i, end2)){
return true;
}
if((isScr(c1, c2, start1, start1+i, end2-i, end2) && isScr(c1, c2, start1+i, end1, start2, end2-i))){
return true;
}
}
return false;
}
}
leetCode 87.Scramble String (拼凑字符串) 解题思路和方法的更多相关文章
- [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 (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- 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 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- Windows 10 作为无线显示器无法被搜索到
症状描述: Windows 10 的投影到此电脑功能失效,但是其它功能正常.同一网络,室友的电脑正常. 解决办法: 设备管理器启用“Microsoft Wi-Fi Direct Virtual Ada ...
- priority_queue的运算符重载问题
对于需要比较的函数或STL(最常见的为sort,priority_queue) 要对自创的结构进行运算符重载(sort可以写cmp,一样的效果) 1.只能对小于号重载 cmp函数与其起到相同的作用 2 ...
- 【kd-tree】bzoj1176 [Balkan2007]Mokia
裸题不多说,注意在sqrt(n*log(n))次插入后重构树以保持深度. #include<cstdio> #include<cmath> #include<algori ...
- 【点分治】【哈希表】bzoj2599 [IOI2011]Race
给nlog2n随便过的跪了,不得已弄了个哈希表伪装成nlogn(当然随便卡,好孩子不要学)…… 不过为啥哈希表的大小开小点就RE啊……?必须得超过数据范围一大截才行……谜 #include<cs ...
- 回文数 Exercise06_03
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:回文数 * */ public class Exercise06_03 { ...
- dwz中弹出的窗口页面如何获取前页面(点击按钮的页面)的元素???
在页面A.jsp中点击一个按钮,使用$.pdialog.open()方法弹出b.jsp页面(对话框窗口),我要在b.jsp中选中值然后关闭窗口(b.jsp)返回值给A.jsp~ =========== ...
- 深入理解Hadoop集群和网络【转】
http://os.51cto.com/art/201211/364374.htm 本文将着重于讨论Hadoop集群的体系结构和方法,及它如何与网络和服务器基础设施的关系.最开始我们先学习一下Hado ...
- 怎样使用libcurl获取隐藏了文件后缀的url网络文件类型
CURLINFO_CONTENT_TYPE CURL: Get Returned Content Mime Type 例如 :以下代码可以查询出天地图的tile图像类型为jpg "http: ...
- PHPCMS V9管理员password忘记怎样改动
一般的虚拟主机商都提供了PHPmyAdmin,选择你站点数据库.然后选择v9_admin这个表. 编辑 password,变成:fa3250300be9b7ab0848257f3cbb06e7 enc ...
- dubbo方法调用的timeout设置
方法调用的默认超时时间为1s,但是具体的超时时间受限于服务端方法性能.服务端个数.客户端的并发数等因素,所以超时时间需要根据不同的场景进行调试. 基本步骤为: 测试服务端的TPS,单位为 任务数或线程 ...