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. wxWidgets界面开发工具wxFormBuilder的使用

    wxFormBuilder 下载地址:http://download.csdn.net/detail/lsmallstop/7013401 安装完成后,打开wxFormBuilder,可以在左侧工程子 ...

  2. 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 【转】

    http://www.cnblogs.com/powertoolsteam/p/MVC_two.html 通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上 ...

  3. oracle手动修改listener.ora和tnsnames.ora之后竟然无效

    oracle手动修改listener.ora和tnsnames.ora之后竟然无效 花式重启都没有生效,需要使用Net Configuration Assistant来进行刷一下,重新生成的监听还是一 ...

  4. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...

  5. oracle软件安装完毕之后,如何创建数据库

    oracle软件安装完毕之后,如何创建数据库 学习了:https://zhidao.baidu.com/question/1800966379896476147.html 使用了Database Co ...

  6. windows中静态库lib和动态dll的区别及使用方法

    1. 静态库lib和动态dll的区别 1.1 项目类型 VS在建Win32项目时,有以下选项: windows应用程序控制台应用程序DLL静态库最后两个类型:DLL和静态库,这两种项目类型是不可以单独 ...

  7. mysql数据库解决中文乱码问题

    安装mysql之后.假设存储中文.再读出的时候就会出现乱码问题. 如今的字符集有几百种之多,都是一些公司或者组织定义的. 我们应该使用可以容纳世界所有语言所有字符的字符集,这样就不会再出现乱码问题. ...

  8. hive分区(partition)简介

    一.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表 ...

  9. 设计模式之MVC设计模式初阶

    MVC M:Model(数据) V:View(界面) C:Control(控制) 1⃣️Control可以直接访问View和Model 2⃣️View不可以拥有Control和Model属性,降低耦合 ...

  10. css 使表格随着内容自动适应宽度

    所谓难而不会,会儿不难.这个问题让我纠结了很长时间,一句css解决了,仅仅靠一个属性 td { white-space: nowrap; }