[LeetCode] Scramble String(树的问题最易用递归)
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.
- class Solution {
- public:
- bool isScramble(string s1, string s2) {
- if(s1.size()== || s2.size()==)
- return false;
- if(s1 == s2)
- return true;
- string a1 = s1,a2 = s2;
- sort(a1.begin(),a1.end());
- sort(a2.begin(),a2.end());
- if(a1!= a2)
- return false;
- int len = s1.size();
- for(int n = ;n < len;n++){
- if(isScramble(s1.substr(,n),s2.substr(,n)) && isScramble(s1.substr(n,len-n),s2.substr(n,len-n)))
- return true;
- if(isScramble(s1.substr(,n),s2.substr(len-n,n)) && isScramble(s1.substr(n,len-n),s2.substr(,len-n)))
- return true;
- }//end for
- return false;
- }//end func
- };
[LeetCode] Scramble String(树的问题最易用递归)的更多相关文章
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode]Scramble String @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String 字符串 dp
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- BZOJ3651 : 网络通信
同[ZJOI2012]网络,把每个点拆成C个点然后用LCT维护. #include<cstdio> #include<map> #define P make_pair #def ...
- (centos)linux下访问双系统windows7文件系统
fdisk -l 无法挂载 NTFS格式的分区:mount: unknown filesystem type ‘ntfs’. 问题: # mount –t ntfs /dev/sdb1 ...
- android 获取当前屏幕作为毛玻璃模糊背景Acitivity作为弹出框。
使用: 1.在执行弹出界面前,先将其当前屏幕截图. BlurBuilder.snapShotWithoutStatusBar(getActivity()); 2.为了确保界面切入无效果. startA ...
- Java之美[从菜鸟到高手演变]之HashMap、HashTable(转载)
http://blog.csdn.net/zhangerqing/article/details/8193118
- [zt]给你的Mp4大换血,精选Touch里3年收集的900多首歌,"经典不忍去的""最新近流行的",与你共享~~
如果你是音乐爱好者: 这些歌, 请戴上耳机, 调大音量, 一个人听 ,全世界 都是你的!!!!! (一)这些歌很温暖,没有金属味,适合有阳光的午后,很悠闲... [Anaesthesia]Maximi ...
- Qt resizeEvent 控件居中设置
在Qt中我们有时需要让一个控件在窗口居中或是在父控件中居中,而且在窗口拉伸时仍然保持正中央的位置.这里介绍一种方法,用到了主窗口的响应函数resizeEvent(QResizeEvent* event ...
- 连接access的语句
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- ThinkPHP之APP_DEBUG给我带来的问题
1.刚开始学习Thinkphp,在模块分组之后,自己配置了模块分组后,发现打不开网页了,分组配置如图 2.问题现象如图 在处理空模块时写的函数也不能运行 这时我很困惑,一直检查自己的拼写和配置没发现错 ...
- php 经典分页(推荐和laypage配合)
学习地址:http://www.imooc.com/video/2463 <?php //(ps:推荐使用laypage整站式跳转来渲染分页按钮样式比较舒服http://laypage.layu ...
- redis 配置 linux
附件 启动停止脚本 redis_6433: #/bin/sh #Configurations injected by install_server below.... EXEC=/usr/local/ ...