087 Scramble String 扰乱字符串
给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树。
下图是字符串s1 = "great"的一种可能的表示形式。
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然后交换它的两个子节点。
例如:如果我们挑选非叶节点 "gr",交换它的两个子节点,将会产生扰乱字符串"rgeat"。
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
我们将"rgeat”称作"great"的一个扰乱字符串。
相同的,如果我们继续将其节点 "eat" 和 "at" 进行交换,将会产生另一个新的扰乱字符串"rgtae"。
rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a
我们将"rgtae”称作"great"的一个扰乱字符串。
给出两个等长的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。
详见:https://leetcode.com/problems/scramble-string/description/
public class Solution {
public boolean isScramble(String s1, String s2) {
if (s1.equals(s2)){
return true;
}
int[] letters = new int[26];
for (int i=0; i<s1.length(); ++i) {
++letters[s1.charAt(i)-'a'];
--letters[s2.charAt(i)-'a'];
}
for (int i=0; i<26; ++i) {
if (letters[i]!=0){
return false;
}
}
for (int i=1; i<s1.length(); i++) {
if (isScramble(s1.substring(0,i), s2.substring(0,i))&&isScramble(s1.substring(i), s2.substring(i))){
return true;
}
if (isScramble(s1.substring(0,i), s2.substring(s2.length()-i))
&& isScramble(s1.substring(i), s2.substring(0,s2.length()-i))){
return true;
}
}
return false;
}
}
参考:https://www.cnblogs.com/grandyang/p/4318500.html
087 Scramble String 扰乱字符串的更多相关文章
- [LintCode] 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] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Java for LeetCode 087 Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Swift]LeetCode87. 扰乱字符串 | Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- LeetCode(87):扰乱字符串
Hard! 题目描述: 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. gr ...
- 45. Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- ansible j2文件注意事项
j2文件中使用变量的时候,不需要 {{}} 遇到if,for等条件判断语句,变量的话,只需要变量名即可, 字符串需要 加上引号.
- PGTM通用性能测试模型
PTGM通用性能测试模型 一. 测试前期准备阶段 目标: 1. 保证系统稳定性: 2. 建立合适的测试团队. 活动: 1. 系统基础功能验证 类似于BVT测试,确保被测系统已具备进行性 ...
- spring mvc提交日期类型参数
如题,spring mvc直接提交Date类型参数会报错,400 bad request的错误.在controller里加上 @InitBinder protected void initBinder ...
- [转载]Android版本更新与JSON解析
/* *注意,这篇文章转载自: *http://blog.csdn.net/xjanker2/article/details/6303937 *一切权利归作者所有,这里只是转载,曾经用到过这篇文 ...
- bootstrap 学习笔记(3)---- 代码
这节讲的是代码: 1.基本实例 <code></code> <pre></pre> <kbd></kbd> 应用如下 1.Fo ...
- CodeForces669E:Little Artem and Time Machine(CDQ分治)(或者用map+树状数组优美地解决)
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of cour ...
- 解决Exception:Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext
原因是配置文件中 <bean id="transactionManager" class="org.springframework.orm.hibernate4.H ...
- python3 分布式进程(跨机器)BaseManager(multiprocessing.managers)
A机器负责发送任务和接受结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #ta ...
- 关于ubuntu 16.04 docker常用命令
1.sudo docker ps -a 查看当前docker实例的信息: CONTAINER ID IMAGE COMMAND ...
- Jquery获取web窗体关闭事件,排除刷新页面
在js脚本里全局定义一个 var r=true;若是刷新的话则把r=false; $(window).unload(function () { if (r) { //这里面证明用户不是点的F5刷新 执 ...