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. Create Data Block Based On From Clause Query In Oracle Forms

    Example is given below to create a data block based on From Clause query in Oracle Forms. The follow ...

  2. dede实战系统:更换成kindEditor编辑器

    最近由于项目需要,在搞dedeCMS,发现dede自带的ckEditor在word粘贴这方面做得不是很好,从word上面直接ctrl+V的内容跟wrod上面的表现样式相差很大,客户很不爽(因为是编辑的 ...

  3. hdu4493(C++)

    //卡格式的题目 #include<iostream> #include<iomanip>using namespace std;int main(){ int T,i; do ...

  4. openfire常见几类插件开发研究与总结

    openfire 的插件可以访问所有openfire的API,这给我们的插件实现提供了巨大的灵活性. 以下介绍几类比较常用的插件集成方式: 基于源码XMPP协议的插件 比如:IQHandler,常用来 ...

  5. php创建无限极目录代码

    php创建无限极目录代码 <?php //无限级目录 function dirs($path,$mode=0777){ if(is_dir($path)){ echo '目录已经存在!'; }e ...

  6. Oracle中group by 的扩展函数rollup、cube、grouping sets

    Oracle的group by除了基本使用方法以外,还有3种扩展使用方法,各自是rollup.cube.grouping sets.分别介绍例如以下: 1.rollup 对数据库表emp.如果当中两个 ...

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

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

  8. Android Studio/IntelliJ IDEA使用手记

      使用第三方jar包 1.将jar包放入项目里的libs文件夹中: 2.在project选中该jar包,点击右键选择:"Add as library": 1. 代码中中文显示乱码 ...

  9. PCA的数学原理Matlab演示

    关于 PCA(Principal component analysis)主成分分析.是SVD(Singular value decomposition)神秘值分析的一种特殊情况.主要用于数据降维.特征 ...

  10. c# emit 实现类的代理

    using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; namespace Em ...