题目:

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.

完全没有思路。

卡了很久,最后参考这里的解释。写了一个递归的版本解决。

代码:

  1. public boolean isScramble(String s1, String s2) {
  2. // Start typing your Java solution below
  3. // DO NOT write main() function
  4. if(!isContainSameChars(s1, s2))return false;
  5. if(s1.equals(s2)) return true;
  6. for(int split = 1; split < s1.length(); split++){
  7. String s11 = s1.substring(0, split);
  8. String s12 = s1.substring(split);
  9.  
  10. String s21 = s2.substring(0, split);
  11. String s22 = s2.substring(split);
  12. if(isScramble(s11, s21) && isScramble(s12, s22)) return true;
  13.  
  14. s21 = s2.substring(0, s2.length() - split);
  15. s22 = s2.substring(s2.length() - split);
  16. if(isScramble(s11, s22) && isScramble(s12, s21)) return true;
  17. }
  18. return false;
  19. }

其实不算难。除非你想不到递归。

LeetCode 笔记系列 19 Scramble String [合理使用递归]的更多相关文章

  1. LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]

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

  2. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  3. LeetCode 笔记系列五 Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  4. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  5. LeetCode之“动态规划”:Scramble String

    题目链接 题目要求: Given a string s1, we may represent it as a binary tree by partitioning it to two non-emp ...

  6. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  7. LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  8. LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  9. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

随机推荐

  1. 二级菜单jquery

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. hdu 2818 Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. Tomcat数据库连接池的配置方法总结

    Tomcat数据库连接池的配置方法总结 数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标 ...

  4. 转--CSS选择器详解(一)常用选择器

    今天复习一下CSS的知识,看了篇文章觉得很好,转来备用. 转自:http://www.cnblogs.com/fattydoit/p/3492028.html 目录 类型选择器 类选择器 ID选择器 ...

  5. 《Play for Java》学习笔记(六)文件上传file upload

    一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...

  6. jQuery Easing动画效果扩展(转)

    jQuery API提供了简单的动画效果如淡入淡出以及自定义动画效果,而今天我给大家分享的是一款jQuery动画效果扩展增强插件jquery.easing.js,使用该插件可以实现直线匀速运功.变加速 ...

  7. Linux查看程序端口占用情况【转】

    今天发现服务器上Tomcat 8080端口起不来,老提示端口已经被占用. 使用命令: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat ...

  8. AFNetworking框架使用

    本文是由 iOS Tutorial 小组成员 Scott Sherwood撰写,他是一个基于位置动态加载(Dynamically Loaded)的软件公司(专业的混合定位)的共同创办人. 网络 — 你 ...

  9. 可伸缩的textview。

    在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容. 这样 ...

  10. [css]邮件的写法

    <style type="text/css">        /* Client-specific Styles */        #outlook a{paddin ...