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":

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".

解题思路:

这道题用的是递归遍历的方法,JAVA实现如下:

static public boolean isScramble(String s1, String s2) {
if (!isMatch(s1, s2))
return false;
if (s1.length() == 0)
return true;
else if (s1.length() == 1) {
if (s1.charAt(0) == s2.charAt(0))
return true;
return false;
} else if (s1.length() == 2) {
if (s1.charAt(0) == s2.charAt(0) && s1.charAt(1) == s2.charAt(1))
return true;
else if (s1.charAt(1) == s2.charAt(0)
&& s1.charAt(0) == s2.charAt(1))
return true;
return false;
}
for (int i = 0; i < s1.length() - 1; i++) {
if (isScramble(s1.substring(0, i + 1), s2.substring(0, i + 1))
&& isScramble(s1.substring(i + 1, s1.length()),
s2.substring(i + 1, s1.length())))
return true;
if (isScramble(s1.substring(0, i + 1),
s2.substring(s1.length()-i-1, s1.length()))
&& isScramble(s1.substring(i+1, s1.length()),
s2.substring(0, s1.length()-i-1)))
return true;
}
return false;
} static public boolean isMatch(String s1, String s2) {
if (s1.length() != s2.length())
return false;
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;
return true;
}

Java for LeetCode 087 Scramble String的更多相关文章

  1. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  4. [LeetCode] 87. Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  5. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  8. 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 ...

  9. leetcode[86] Scramble String

    将一个单词按照这种方式分: Below is one possible representation of s1 = "great": great / \ gr eat / \ / ...

随机推荐

  1. 【GLSL教程】(七)逐像素的光照 【转】

    http://blog.csdn.net/racehorse/article/details/6662540 逐像素的方向光(Directional Light per Pixel) 这一节将把前面的 ...

  2. 转: 三大WEB服务器对比分析(apache ,lighttpd,nginx) (2008年的旧文,仅供参考之用)

    from:  http://www.blogjava.net/daniel-tu/archive/2008/12/29/248883.html 三大WEB服务器对比分析(apache ,lighttp ...

  3. ArcGIS教程:面积制表

    摘要 计算两个数据集之间交叉制表的区域并输出表. 插图 使用方法 · 区域定义为输入中具有同样值的全部区.各区无需相连. 栅格和要素数据集都可用于区域输入. · 假设区域输入和类输入均为具有同样分辨率 ...

  4. Java架构师与开发者提高效率的10个工具

    Java受到全球百万计开发者的追捧,已经演变为一门出色的编程语言.最终,这门语言随着技术的变化,不断的被改善以迎合变化的市场需求. 无论你是否拥有一家科技公司,软件已经成为几乎每一个企业不可或缺的一部 ...

  5. apache TIME_WAIT解决办法

    最近发现apache与负载均衡器的的连接数过多,而且大部分都是TIME_WAIT,调整apache2.conf后也没效果,最后百度到如下解决方案 通过调整内核参数解决 vi /etc/sysctl.c ...

  6. ASP.NET MVC深入浅出系列(持续更新) ORM系列之Entity FrameWork详解(持续更新) 第十六节:语法总结(3)(C#6.0和C#7.0新语法) 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字 各种通讯连接方式 设计模式篇 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借

    ASP.NET MVC深入浅出系列(持续更新)   一. ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态模 ...

  7. Linux禁止Ctrl+Alt+Del重新启动

    方法1:改动/etc/inittab 屏蔽 ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now 或者删除改行内容 保存退出 适用对象:RedHat4.8 ...

  8. 六种基本DCDC变换器拓扑结构

    1.SEPIC电路 2.

  9. php计算两个经纬度地点之间的距离(转)

    php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...

  10. jquery瀑布流布局插件,兼容ie6不支持下拉加载,用于制作分类卡

    调用方法 $('需要布局的块').sault() 如果要在图片加载后调用需要使用$(window).load(function(fx){});函数,即等待图片加载完成再调用 3个参数 1.left:左 ...