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. class Solution {
  2. public:
  3. bool isScramble(string s1, string s2) {
  4. if(s1.size()== || s2.size()==)
  5. return false;
  6. if(s1 == s2)
  7. return true;
  8. string a1 = s1,a2 = s2;
  9. sort(a1.begin(),a1.end());
  10. sort(a2.begin(),a2.end());
  11. if(a1!= a2)
  12. return false;
  13. int len = s1.size();
  14. for(int n = ;n < len;n++){
  15. if(isScramble(s1.substr(,n),s2.substr(,n)) && isScramble(s1.substr(n,len-n),s2.substr(n,len-n)))
  16. return true;
  17. if(isScramble(s1.substr(,n),s2.substr(len-n,n)) && isScramble(s1.substr(n,len-n),s2.substr(,len-n)))
  18. return true;
  19.  
  20. }//end for
  21. return false;
  22. }//end func
  23. };

[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. BZOJ3651 : 网络通信

    同[ZJOI2012]网络,把每个点拆成C个点然后用LCT维护. #include<cstdio> #include<map> #define P make_pair #def ...

  2. (centos)linux下访问双系统windows7文件系统

    fdisk -l 无法挂载 NTFS格式的分区:mount: unknown filesystem type ‘ntfs’.   问题:       # mount –t ntfs /dev/sdb1 ...

  3. android 获取当前屏幕作为毛玻璃模糊背景Acitivity作为弹出框。

    使用: 1.在执行弹出界面前,先将其当前屏幕截图. BlurBuilder.snapShotWithoutStatusBar(getActivity()); 2.为了确保界面切入无效果. startA ...

  4. Java之美[从菜鸟到高手演变]之HashMap、HashTable(转载)

    http://blog.csdn.net/zhangerqing/article/details/8193118

  5. [zt]给你的Mp4大换血,精选Touch里3年收集的900多首歌,"经典不忍去的""最新近流行的",与你共享~~

    如果你是音乐爱好者: 这些歌, 请戴上耳机, 调大音量, 一个人听 ,全世界 都是你的!!!!! (一)这些歌很温暖,没有金属味,适合有阳光的午后,很悠闲... [Anaesthesia]Maximi ...

  6. Qt resizeEvent 控件居中设置

    在Qt中我们有时需要让一个控件在窗口居中或是在父控件中居中,而且在窗口拉伸时仍然保持正中央的位置.这里介绍一种方法,用到了主窗口的响应函数resizeEvent(QResizeEvent* event ...

  7. 连接access的语句

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. ThinkPHP之APP_DEBUG给我带来的问题

    1.刚开始学习Thinkphp,在模块分组之后,自己配置了模块分组后,发现打不开网页了,分组配置如图 2.问题现象如图 在处理空模块时写的函数也不能运行 这时我很困惑,一直检查自己的拼写和配置没发现错 ...

  9. php 经典分页(推荐和laypage配合)

    学习地址:http://www.imooc.com/video/2463 <?php //(ps:推荐使用laypage整站式跳转来渲染分页按钮样式比较舒服http://laypage.layu ...

  10. redis 配置 linux

    附件 启动停止脚本 redis_6433: #/bin/sh #Configurations injected by install_server below.... EXEC=/usr/local/ ...