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 ( ...
随机推荐
- python 练习 7
#!/usr/bin/python # -*- coding: utf-8 -*- def gcd(x,y): #最大公因子 if x>y:x%=y while x: x,y=y%x,x ret ...
- 在Windows8.1中通过IIS发布网站产生HTTP Error 503错误的解决方案
1.解决IIS下网站Bin目录中32位DLL不能使用,如图所示 2.解决通过IIS浏览网站,出现Http503的问题,如图所示
- android 模拟器 使用键盘的配置
1 打开 android Manageer , 克隆一个设备 2.
- Spring学习(一)——Spring中的依赖注入简介【转】
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- weblogic部署ssh2应用出现异常
一般在domains\your_Domain\bin\startWebLogic.cmd 中找到set CLASSPATH=%CLASSPATH%;%MEDREC_WEBLOGIC_CLASSPATH ...
- myeclipse 6.5配置tomcat7.X
软件安装:myeclipse 6.5 Apache Tomcat/7.0.47 安装完成后开始配置 1. 随便展开一个tomcat配置选项,这里配置到tomcat6.x,如下图:
- 腾讯大规模Hadoop集群实践 [转程序员杂志]
TDW(Tencent distributed Data Warehouse,腾讯分布式数据仓库)基于开源软件Hadoop和Hive进行构建,打破了传统数据仓库不能线性扩展.可控性差的局限,并且根据腾 ...
- 字符串转化为json方法
1.function strToJson(str){ var json = eval('(' + str + ')'); return json; } 不过eval解析json有安全隐患! 现在大多数 ...
- LAMP之安装mysql/apache/php
1. 安装mysqlcd /usr/local/src/ wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-i686-gl ...
- TextBoxButton控件的开发实现
效果图: 实现代码: public TextBoxButton() { _button = new Button { ForeColor = System.Drawing.SystemColors.G ...