LeetCode 笔记系列 19 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.
完全没有思路。
卡了很久,最后参考这里的解释。写了一个递归的版本解决。
代码:
public boolean isScramble(String s1, String s2) {
// Start typing your Java solution below
// DO NOT write main() function
if(!isContainSameChars(s1, s2))return false;
if(s1.equals(s2)) return true;
for(int split = 1; split < s1.length(); split++){
String s11 = s1.substring(0, split);
String s12 = s1.substring(split); String s21 = s2.substring(0, split);
String s22 = s2.substring(split);
if(isScramble(s11, s21) && isScramble(s12, s22)) return true; s21 = s2.substring(0, s2.length() - split);
s22 = s2.substring(s2.length() - split);
if(isScramble(s11, s22) && isScramble(s12, s21)) return true;
}
return false;
}
其实不算难。除非你想不到递归。
LeetCode 笔记系列 19 Scramble String [合理使用递归]的更多相关文章
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 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 ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- 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. ...
- LeetCode之“动态规划”:Scramble String
题目链接 题目要求: Given a string s1, we may represent it as a binary tree by partitioning it to two non-emp ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- 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 ...
- 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 ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
随机推荐
- div中字垂直居中对齐
div中的文本水平居中,一般都是用text-align:center;就可以解决,那么垂直居中呢,知道vertiacl-align:middle;但有时候却不起作用:整理下div中文本垂直居中对齐的问 ...
- 《Java程序设计》第六周学习总结
20145224 <Java程序设计>第六周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·若要将数据从来源中取出,可以使用输入串流:若要将数据写入目的地,可以使用输出串流. ...
- BluetoothGatt API
punlic final class BluetoothGatt继承自Object , 实现了BluetoothProfile接口/** 相关的蓝牙协议可http://www.cnki.net/KCM ...
- LDM和STM指令
LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond} mode Rn{!} ...
- iOS 开发 – 均衡代码职责
前言 文章的标题有点绕口,不过想了半天,想不到更好的标题了.本文的诞生有一部分功劳要归于iOS应用现状分析,标题也是来源于原文中的"能把代码职责均衡的划分到不同的功能类里".如果你 ...
- soapUI参数
点击File->New Rest Project,填入要测试的URI,确定进入编辑界面: 调整请求方式,添加请求参数,设置参数风格,这里要说一下:style有五种,QUERY是默认常用:TEMP ...
- 减少HTTP请求之将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片(大型网站优化技术)
在网站开发过程中,对于页面的加载效率一般都想尽办法求快.那么,怎么让才能更快呢?减少页面请求 是一个优化页面加载速度很好的方法.上一篇博文我们讲解了 “利用将小图标合成一张背景图来减少HTTP请求”, ...
- 436. Find Right Interval ——本质:查找题目,因此二分!
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- visual2012 快捷键
代码格式化:Ctrl+K+D 注释:VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C) 反注释:VS2010是(Ctrl+E,U),VS2012是(Ctrl+K, Ct ...
- BZOJ3996 [TJOI2015]线性代数
就是求$D = A \times B \times A^T - C \times A^T$ 展开也就是$$D = \sum_{i, j} A_i * A_j * B_{i, j} - \sum_{i} ...