题目:

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 [合理使用递归]的更多相关文章

  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. 【C#】获取当前系统桌面、我的照片、我的文档等路径

    获取当前系统桌面.我的照片.我的文档等路径   using System; using System.Collections.Generic; using System.ComponentModel; ...

  2. [Java] Java 获取数据库所有表基本信息和表中的所有列基本信息代码

    废话不多说.上代码 import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import ...

  3. MySQL学习笔记_1_MySQL数据库管理系统概述

    1. MySQL架构 C/S: client / server架构 MySQL DBMS(Data Bank Management System): 数据库管理系统 客户端 <---> 服 ...

  4. hdu----(5047)Sawtooth(大数相乘+数学推导)

    Sawtooth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  6. 整理的一些模版LCS(连续和非连续)

    对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...

  7. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. SAP客户标准信用额度修改和创建

    好吧,前提要说一下,信贷是到客户的信贷范围级别的. FUNCTION zfm_credit. *"---------------------------------------------- ...

  9. MVC扩展ValueProvider,通过实现IValueProvider接口创建SessionValueProvider

    □ ValueProvider的大致工作原理 →通过Request.Form, Request.QueryString, Request.Files, RouteData.Values获取数据.→然后 ...

  10. 语句:分支语句、switch case ——7月22日

    语句的类型包括:声明语句.表达式语句.选择语句.循环语句.跳转语句.异常语句 1.声明语句引:入新的变量或常量. 变量声明可以选择为变量赋值. 在常量声明中必须赋值. 例如: int i = 0;// ...