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 ...
随机推荐
- x264 --fullhelp
>x264 --fullhelp x264 core: Syntax: x264 [options] -o outfile infile Infile can be raw (in which ...
- hdu-5003 Osu!(水题)
题目链接: Osu! time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- 机器学习:Principal components analysis (主分量分析)
Principal components analysis 这一讲,我们简单介绍Principal Components Analysis(PCA),这个方法可以用来确定特征空间的子空间,用一种更加紧 ...
- 如何判断一个for循环执行完毕
在外面一个变量a=arr.leng; 然后就是进行for循环, 在for循环下面进行判断,因为如果结束那么i的值就会>=a;if条件成立的话,可以在里面进行循环完毕要做的操作.
- java-执行dos命令
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOExce ...
- Tensorboard 的简单使用
确保环境以及安装好tensorflow以及tensorboard 下面通过一个简单的例子来显示一下使用方式,一个向量加法的图结构. import tensorflow as tf a = tf.con ...
- DataWindow.NET 控件 实现点击列头排序
1.定义字段 Boolean ib_SetSort = true; string is_SortType = " ...
- 1 python----pycharm本地部署spark
下图相关工具连接 链接:https://pan.baidu.com/s/115XWf_Fc1yMiJytKJQXnFQ 密码:3jvr 好了,加油哟!
- 利用python数据分析panda学习笔记之DataFrame
2 DataFrame a:通过传入一个等长的列表构成DataFrame 自动加上索引 data={'state':['ohio','ohio','ohio','Nevada','Nevada'], ...
- 微信小程序开发之页面数据绑定
js:Page( { data:{ parmer:"", //字符串参数 userinfo:{ userphone:"", ...