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的更多相关文章

  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] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  3. [LeetCode] Scramble String 爬行字符串

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

  4. [leetcode]Scramble String @ Python

    原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...

  5. [LeetCode] 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 乱串

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

  7. [LeetCode] Scramble String 字符串 dp

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

  8. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【leetcode】Scramble String

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

随机推荐

  1. Android Matrix

    转自 :http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#code Matrix的数学原理 平移变换 旋转变换 缩放变换 错切 ...

  2. 你可能不知道的java、python、JavaScript以及jquary循环语句的区别

    一.概述 java循环语句分为四种形式,分别是 while, do/while, for, foreach: python中循环语句有两种,while,for: JavaScript中循环语句有四种, ...

  3. test1.A[【dfs简单题】

    Test1.A Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 sdut 2274:http://acm.sdut.edu.cn/ ...

  4. Spell checker

     Spell checker Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  5. select * from salgrade for update和select * from salgrade for update nowait区别

    1,select * from salgrade for update session1 session2 SQL> delete salgrade where grade=1; 1 row d ...

  6. [Tools] 使用XP远程登录Win8系统

    [背景] 完成最基本的设置后,发现xp依然不能远程访问win8桌面,搜索后发现需要进一步设置   [开工] 按照参考资料进行设置,下面的参考资料已经写的很详细了,只是参考资料2中的文件名: redss ...

  7. map[C++]

    //map是一个存储键值对的容器,也是一个双向链表 #include <iostream> using namespace std; #include <map> int ma ...

  8. Practical JAVA(二)关于对象的类型和equals函数

    Practice5,6,9,10,11,12,13,14,15 ==判断等号两边两个变量储存的值是否相同,如果是两个对象,则判断两个变量储存的对象地址是否相同. 大多数时候,我们需要判断的不是左右两个 ...

  9. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  10. Codeforces Round #356 (Div. 2)-A

    A. Bear and Five Cards 题目链接:http://codeforces.com/contest/680/problem/A A little bear Limak plays a ...