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. 浅谈XXE攻击

    一.XXE,即XML External Entity,XML外部实体.ENTITY 实体,在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的“别名”,即一个ENTITY,然后 ...

  2. git错误解决 -- 小结

    1.今天 当我  执行  Git add  somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...

  3. 又一次遇到Data truncation: Data too longData truncation: Data too long问题

    往MySQL的blob字段上传文件,结果又出现了Data truncation: Data too longData truncation: Data too long异常. 我的第一反应是查看/et ...

  4. Android4.0(Phone)拨号启动过程分析(一)

    因为工作的须要.须要改动原生的Phone程序,如今就好好看下来电与拨号是怎样处理的:无论是拨号还是来电,调用的都是Phone程序,因为非常多类都涉及到framework层,比較复杂:先从简单的拨号分析 ...

  5. vue-cli配置文件详解

    转自: https://blog.csdn.net/Mr_YanYan/article/details/79233188

  6. HDU 4746 Mophues (莫比乌斯反演应用)

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total ...

  7. Interleaving String——是否由两个string交叉、DP

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  8. Backup and Recovery Basics2

    1.6.Automatic Disk-Based Backup and Recovery: The Flash Recovery Area 创建不同备份和恢复文件的组件对每一个文件系统的大小没有不论什 ...

  9. xshell的快捷键(很有用)

    删除 ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h      删除光标所在位置前的字符相当于VIM里hx或者dh ctrl + k      删除光标 ...

  10. 动态载入Layout 与 论Activity、 Window、View的关系

    1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...