题目

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

  1. great
  2. / \
  3. gr eat
  4. / \ / \
  5. g r e at
  6. / \
  7. 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".

  1. rgeat
  2. / \
  3. rg eat
  4. / \ / \
  5. r g e at
  6. / \
  7. 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".

  1. rgtae
  2. / \
  3. rg tae
  4. / \ / \
  5. r g ta e
  6. / \
  7. 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.

题解

这道题完全转载code ganker(http://blog.csdn.net/linhuanmars/article/details/24506703):

“这道题看起来是比较复杂的,如果用brute force,每次做切割,然后递归求解,是一个非多项式的复杂度,一般来说这不是面试官想要的答案。
这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble。

了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i][j][len]。判断这个是不是满足,其实我们首先是把当前
s1[i...i+len-1]字符串劈一刀分成两部分,然后分两种情况:第一种是左边和s2[j...j+len-1]左边部分是不是
scramble,以及右边和s2[j...j+len-1]右边部分是不是scramble;第二种情况是左边和s2[j...j+len-1]右边部
分是不是scramble,以及右边和s2[j...j+len-1]左边部分是不是scramble。如果以上两种情况有一种成立,说明
s1[i...i+len-1]和s2[j...j+len-1]是scramble的。而对于判断这些左右部分是不是scramble我们是有历史信息
的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
上面说的是劈一刀的情况,对于s1[i...i+len-1]我们有len-1种劈法,在这些劈法中只要有一种成立,那么两个串就是scramble的。

结起来递推式是res[i][j][len] = || (res[i][j][k]&&res[i+k][j+k][len-k]
|| res[i][j+len-k][k]&&res[i+k][j][len-k])
对于所有1<=k<len,也就是对于所有len-1种劈法的结果求或运算。因为信息都是计算过的,对于每种劈法只需要常量操作即可完成,因
此求解递推式是需要O(len)(因为len-1种劈法)。
如此总时间复杂度因为是三维动态规划,需要三层循环,加上每一步需要线行时间求解递推式,所以是O(n^4)。虽然已经比较高了,但是至少不是指数量级的,动态规划还是有很大有事的,空间复杂度是O(n^3)。代码如下:”

代码如下:

  1.  1 public boolean isScramble(String s1, String s2) {
  2.  2     if(s1==null || s2==null || s1.length()!=s2.length())
  3.  3         return false;
  4.  4     if(s1.length()==0)
  5.  5         return true;
  6.  6     boolean[][][] res = new boolean[s1.length()][s2.length()][s1.length()+1];
  7.  7     for(int i=0;i<s1.length();i++)
  8.  8     {
  9.  9         for(int j=0;j<s2.length();j++)
  10.          {
  11.              res[i][j][1] = s1.charAt(i)==s2.charAt(j);
  12.          }
  13.      }
  14.      for(int len=2;len<=s1.length();len++)
  15.      {
  16.          for(int i=0;i<s1.length()-len+1;i++)
  17.          {
  18.              for(int j=0;j<s2.length()-len+1;j++)
  19.              {
  20.                  for(int k=1;k<len;k++)
  21.                  {
  22.                      res[i][j][len] |= res[i][j][k]&&res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k];
  23.                  }
  24.              }
  25.          }
  26.      }
  27.      return res[0][0][s1.length()];
  28.  }

同样这道题也可以用递归来做。

我个人觉得用递归做更加方便简单些,思路还是上面那个思路,分两刀那种(底下解释引用自:http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ascramble-string%EF%BC%8C%E4%B8%89%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/):

“简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。

那么要么s11和s21是scramble的并且s12和s22是scramble的;要么s11和s22是scramble的并且s12和s21是scramble的。”

代码:

  1.  1 public boolean isScramble(String s1, String s2) {
  2.  2         if(s1.length() != s2.length())  
  3.  3             return false;  
  4.  4         
  5.  5         if(s1.length()==1 && s2.length()==1)
  6.  6             return s1.charAt(0) == s2.charAt(0);  
  7.  7     
  8.  8        char[] t1 = s1.toCharArray(), t2 = s2.toCharArray();
  9.  9        Arrays.sort(t1);
  10.         Arrays.sort(t2);
  11.         if(!new String(t1).equals(new String(t2)))
  12.           return false;
  13.           
  14.         if(s1.equals(s2)) 
  15.           return true;
  16.           
  17.         for(int split = 1; split < s1.length(); split++){
  18.             String s11 = s1.substring(0, split);
  19.             String s12 = s1.substring(split);
  20.             
  21.             String s21 = s2.substring(0, split);
  22.             String s22 = s2.substring(split);
  23.             if(isScramble(s11, s21) && isScramble(s12, s22))
  24.               return true;
  25.             
  26.             s21 = s2.substring(0, s2.length() - split);
  27.             s22 = s2.substring(s2.length() - split);
  28.             if(isScramble(s11, s22) && isScramble(s12, s21))
  29.              return true;
  30.         }
  31.         return false;
  32.      }

Reference:

http://www.cnblogs.com/lichen782/p/leetcode_Scramble_String.html

http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ascramble-string%EF%BC%8C%E4%B8%89%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/

http://blog.csdn.net/fightforyourdream/article/details/17707187

http://blog.csdn.net/linhuanmars/article/details/24506703

Scramble String leetcode java的更多相关文章

  1. Scramble String -- LeetCode

    原题链接: http://oj.leetcode.com/problems/scramble-string/  这道题看起来是比較复杂的,假设用brute force,每次做分割,然后递归求解,是一个 ...

  2. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

  3. Interleaving String leetcode java

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

  4. Leetcode:Scramble String 解题报告

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

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

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

  6. 【leetcode】Scramble String

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

  7. 【LeetCode练习题】Scramble String

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

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

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

  9. [LeetCode] Scramble String -- 三维动态规划的范例

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

随机推荐

  1. SPOJ11414 COT3 博弈论 + Trie树合并

    考虑对于每个子树从下往上依次考虑 对于叶子节点而言,如果可以染色,那么其\(sg\)值为\(1\),否则为\(0\) 考虑往上合并 如果选择了\(x\),那么后继状态就是其所有子树 如果选了其他子树中 ...

  2. [BZOJ4129]Haruna’s Breakfast(树上带修改莫队)

    BZOJ3585,BZOJ2120,BZOJ3757三合一. 对于树上路径问题,树链剖分难以处理的时候,就用树上带修改莫队. 这里的MEX问题,使用BZOJ3585的分块方法,平衡了时间复杂度. 剩下 ...

  3. BZOJ.1018.[SHOI2008]堵塞的交通(线段树维护连通性)

    题目链接 只有两行,可能的路径数不多,考虑用线段树维护各种路径的连通性. 每个节点记录luru(left_up->right_up),lurd,ldru,ldrd,luld,rurd,表示这个区 ...

  4. lvs+keepalived 02

    LVS keepalived 高可用负载均衡 环境 IP HOSTNAME Describe 192.168.100.30 lvs01 主负载 192.168.100.31 lvs02 备负载 192 ...

  5. HDU 5813 Elegant Construction 构造

    Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  6. ROS知识(21)----ROS C++代码格式化

    这里提供两种方法. 第一种方法:clang_format 1.安装clang format sudo apt-get install -y clang-format-3.6 2.从github的ros ...

  7. mysql导入csv文件

    今天尝试将Oracle中的数据导入到mysql中,在SQLyog工具其中看到一些sql语句,拿来记录一下,说不定以后就用的着呐! -----查看ydtf数据库中的基础表,就是用户创建了哪些表 SHOW ...

  8. android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)

    写UI布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  9. 解决ASP.NET MVC4中使用Html.DropDownListFor显示枚举值默认项问题

    从ASP.NET MVC 5开始,Html.DropDownListFor已经提供了对Enum的支持,但在这以前,需要通过帮助方法或扩展方法来让Html.DropDownListFor显示枚举值. 本 ...

  10. 玩一下C#的语音识别

    在.NET4.0中,我可以借助System.Speech组件让电脑来识别我们的声音. 以上,当我说"name",显示"Darren",我说"age&q ...