[Leetcode] 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.
Solution:
public class Solution {
public boolean isScramble(String s1, String s2) {
if(s1.equals(s2))
return true;
int l1=s1.length();
int l2=s2.length();
if(l1!=l2)
return false;
if(l1==0)
return true;
if(l1==1)
return s1.equals(s2);
char[] c_s1=s1.toCharArray();
char[] c_s2=s2.toCharArray();
Arrays.sort(c_s1);
Arrays.sort(c_s2);
for(int i=0;i<c_s1.length;++i){
if(c_s1[i]!=c_s2[i])
return false;
}
boolean b=false;
for(int i=1;i<l1&&!b;++i){
String s11=s1.substring(0,i);
String s12=s1.substring(i);
String s21=s2.substring(0,i);
String s22=s2.substring(i);
b=isScramble(s11, s21)&&isScramble(s12, s22);
if(!b){
String s31=s2.substring(0, l1-i);
String s32=s2.substring(l1-i);
b=isScramble(s11, s32)&&isScramble(s12, s31);
}
}
return b;
}
}
------------------------------------------------------------------------------------
20150220:
public class Solution {
public boolean isScramble(String s1, String s2) {
if(s1==null||s2==null||s1.length()!=s2.length())
return false;
if(s1.equals(s2))
return true;
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
for(int i=0;i<c1.length;++i){
if(c1[i]!=c2[i])
return false;
}
int N=s1.length();
for(int i=1;i<N;++i){
String s1_temp1=s1.substring(0,i);
String s1_temp2=s1.substring(i);
String s2_temp1=s2.substring(0,i);
String s2_temp2=s2.substring(i);
if(isScramble(s1_temp1, s2_temp1)&&isScramble(s1_temp2, s2_temp2))
return true;
String s2_temp3=s2.substring(0,N-i);
String s2_temp4=s2.substring(N-i);
if(isScramble(s1_temp1, s2_temp4)&&isScramble(s1_temp2, s2_temp3))
return true;
}
return false;
}
}
[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 字符串 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 ...
随机推荐
- mysql TIME_WAIT
http://www.th7.cn/system/lin/201404/53762.shtml http://kerry.blog.51cto.com/172631/105233/ http://ww ...
- Jquery自定义图片上传插件
1 概述 编写后台网站程序大多数用到文件上传,可是传统的文件上传控件不是外观不够优雅,就是性能不太好看,翻阅众多文件上传控件的文章,发现可以这样去定义一个文件上传控件,实现的文件上传的效果图如下: 2 ...
- 基于PHP+Ajax实现表单验证的详解
一,利用键盘响应,在不刷新本页面的情况下验证表单输入是否合法 用户通过onkeydown和onkeyup事件来触发响应事件.使用方法和onclick事件类似.onkeydown表示当键盘上的键被按下时 ...
- PowerDesigner(PowerDesigner15.1.0.2850)下载、安装以及破解
转自:http://www.cnblogs.com/Fonkie/articles/1600662.html 一.先安装PowerDesigner15(PowerDesigner15.1.0.2850 ...
- 两个本地(localhost)html文件之间的传值
什么是iframe? iframe 元素会创建包含另外一个文档的内联框架(即行内框架).可以理解为把iframe解释成“浏览器中的浏览器“ 在IE中: document.frames[i].docum ...
- 为什么要使用 Node.js
这是一个移动端工程师涉足前端和后端开发的学习笔记,如有错误或理解不到位的地方,万望指正. Node.js 是什么 传统意义上的 JavaScript 运行在浏览器上,这是因为浏览器内核实际上分为两个部 ...
- Java学习笔记(八)——封装
一.封装 1.定义 将类的信息隐藏在类的内部,不允许外部程序直接进行访问,而是通过该类提供的方法来实现对隐藏信息的操作和方法. 2.优点 (1)只能通过规定的方法访问数据 (2)隐藏类的细节,方便修改 ...
- TextView展开和收回
第一步:接口请求返回数据 第二步:使用handler和textview.getLineCount方法判断是否超过指定行数: community_desc_more.setVisibility(View ...
- a与a:link、a:visited、a:hover、a:active
原文地址http://www.cnblogs.com/exmyth/p/3226654.html a与a:link.a:visited.a:hover.a:active 起因: a与a:link的 ...
- poj3186 Treats for the Cows(区间)
题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...