leetcode — scramble-string
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/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.
*
*/
public class ScrambleString {
/**
* s1是不是s2的一个scramblestring
* s1按照任意位置进行二分划分,一直递归下去,期间,可以交换非叶子节点的两个子节点左右顺序,一直到叶子节点
*
* 一开始想着是找到s1的所有scramblestring,然后判断s2是否在里面
* 但是其实在寻找s2的scramblestring的时候就可以和s2进行对比判断,而不需要存储所有的scramblestring
*
* 选择
* s1分割的位置,递归的进行如下判断
* s1在i左边的子串和s2在i左边的子串是scramble的,s1在i的右边的子串和s2在i右边的子串是scramble的,或者
* s1在i左边的子串和s2在i右边的子串是scramble的,s1在i的右边的子串和s2在i左边的子串是scramble的
*
* @param s1
* @param s2
*/
public boolean scramble (String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1.length() <= 1) {
return s1.equals(s2);
}
// return recursion(s1, s2);
return recursion1(s1, s2);
}
public boolean recursion (String s1, String s2) {
int len = s1.length();
if (len == 1) {
return s1.equals(s2);
}
for (int i = 1; i < len; i++) {
if ((recursion(s1.substring(0, i), s2.substring(0, i)) && recursion(s1.substring(i), s2.substring(i)))
|| (recursion(s1.substring(0,i), s2.substring(len-i)) && recursion(s1.substring(i), s2.substring(0,len-i)))) {
return true;
}
}
return false;
}
/**
* 递归的时候有些分支是不必要的,可以剪裁分支
* 在递归的时候,对s1和s2进行排序,如果排序之后两个字符串不相等则不必要继续递归
*
* @param s1
* @param s2
* @return
*/
public boolean recursion1 (String s1, String s2) {
int len = s1.length();
if (len == 1) {
return s1.equals(s2);
}
char[] s1CharArr = s1.toCharArray();
Arrays.sort(s1CharArr);
String sortedS1 = new String(s1CharArr);
char[] s2CharArr = s2.toCharArray();
Arrays.sort(s2CharArr);
String sortedS2 = new String(s1CharArr);
if (!sortedS1.equals(sortedS2)) {
return false;
}
for (int i = 1; i < len; i++) {
if ((recursion(s1.substring(0, i), s2.substring(0, i)) && recursion(s1.substring(i), s2.substring(i)))
|| (recursion(s1.substring(0,i), s2.substring(len-i)) && recursion(s1.substring(i), s2.substring(0,len-i)))) {
return true;
}
}
return false;
}
/**
* 递归的时候会有一些重复计算,使用数组记录计算过的结果,每次递归的时候判断,如果已经计算过则直接使用计算过的结果
* 中间结果需要一个三维的boolean数组,因为,每次计算结果的变量是s1.index1,s2.index2,还有当前字符串的长度len
*
* @param s1
* @param s2
* @return
*/
public boolean scramble2 (String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1.length() <= 1) {
return s1.equals(s2);
}
int[][][] calculated = new int[s1.length()][s2.length()][s1.length()];
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
Arrays.fill(calculated[i][j], -1);
}
}
return recursion(s1, s2);
}
public boolean recursion2 (String s1, int index1, String s2, int index2, int len, int[][][] calculated) {
if (len == 1) {
return s1.charAt(index1) == s2.charAt(index2);
}
int preresult = calculated[index1][index1][len-1];
if (preresult != -1) {
return preresult == 1;
}
preresult = 0;
for (int i = 1; i < len; i++) {
if (recursion2(s1, index1, s2, index2, i, calculated)
&& recursion2(s1, index1 + 1, s2, index2 + 1, len - i, calculated)) {
preresult = 1;
break;
}
if (recursion2(s1, index1, s2, index2 + len - i, i, calculated)
&& recursion2(s1, index1 + 1, s2, index2, len - i, calculated)) {
preresult = 1;
break;
}
}
calculated[index1][index2][len-1] = preresult;
return preresult == 1;
}
public static void main(String[] args) {
ScrambleString scrambleString = new ScrambleString();
System.out.println(scrambleString.scramble("great", "rgtae"));
System.out.println(scrambleString.scramble2("great", "rgtae"));
}
}
leetcode — scramble-string的更多相关文章
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode]Scramble String @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String(树的问题最易用递归)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String 字符串 dp
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 ...
随机推荐
- Android中View的绘制流程(专题讲解)
Android中的UI视图有两种方式实现:.xml文件(实现代码和UI的分离)和代码实现. Android的UI框架基本概念: 1. Activity:基本的页面单元,Activity包含一个Wind ...
- C++第二课:指针常用法[个人见解]
在小编这里,没有任何学习知识的顺序,写到的东西对初学者肯定是有用处的,前提,你真的把C语言学完的那些初学者. 在讲明指针的知识前,或许有人一直说不会指针你学不会C++,或者说你所学C++的深度,全凭你 ...
- 微信跳转,手机WAP浏览器一键超级跳转微信指定页面
微信跳转,手机WAP浏览器一键超级跳转微信指定页面 这篇文章主要介绍了如何在手机浏览器wap网页中点击链接跳转到微信界面,需要的朋友可以参考下 先说第一种,最简单的唤起微信协议,weixin://主流 ...
- HTTP 报文格式
(a)GET,POST,PUT(更新)DELETE(删除) 首行中,请求报文只需包含路由,因为在发送请求前,tcp连接已经创建,协议版本 Header: 接收类型 (b)版本 + 状态码 Header ...
- Egret的按钮事件处理
首先要在exml内要设置有对应按钮的ID 2,编写TypeScript脚本: public mybutton:eui.Button; 函数内部:this.mybutton.addEventListen ...
- JS DOM事件学习
DOM查找方法: document.getElementByID("id") document.getElementsByTagName("tag") 返回一个 ...
- jQuery倒计时组件(jquery.downCount.js)
//html <span class="days">00</span> <span class="hours">00< ...
- 【原创开源应用第5期】基于RL-USB+RL-FlashFS的外挂U盘解决方案
说明:1.RL-USB外挂U盘的例子,最近太多网友咨询,再不做一个例子就说不过去了.此例子为此而生.2.RTX及其所有中间件基本都做例子了,就差这个USB Host功能了,这次算是补上,所有功能基本已 ...
- Mycat对MySQL进行垂直水平分表分库,读写分离
1. MyCAT概述 1.1 背景 随着传统的数据库技术日趋成熟.计算机网络技术的飞速发展和应用范围的扩充,数据库应用已经普遍建立于计算机网络之上.这时集中式数据库系统表现出它的不足: (1)集中 ...
- Websocket实现即时通讯
前言 关于我和WebSocket的缘:我从大二在计算机网络课上听老师讲过之后,第一次使用就到了毕业之后的第一份工作.直到最近换了工作,到了一家是含有IM社交聊天功能的app的时候,我觉得我现在可以谈谈 ...