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 ...
随机推荐
- IOC入门1
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- 值域线段树 (玲珑OJ 1117)
点击打开题目链接 题目意思很简单: 1.插入x 2.把小于x的数变成x 3.把大于x的数变成x 4.求集合中第x小数 5.求集合中小于x的数个数 思路: 线段树,节点是值的分数,你可以离散,也可以不离 ...
- uglifyjs2全局混淆
从git克隆uglifyjs2源码后,进入目录: npm link 编译并安装uglifyjs2成功,就可以直接调用uglifyjs命令了.但是在进行全局混淆时出现了问题,虽然指定了文件topvar. ...
- 003 - 修改Pycharm的项目文件树样式
相信习惯了Eclipse或者Windows的小伙伴对于Pycharm的目录树一定觉得特别别扭 因为它总是在文件前加一个三角形标注, 这样的标注在视觉上十分误导层级关系 修改的方式为 File -& ...
- Batch Normalization层
Batch Normalization的加速作用体现在两个方面:一是归一化了每层和每维度的scale,所以可以整体使用一个较高的学习率,而不必像以前那样迁就小scale的维度:二是归一化后使得更多的权 ...
- 算法实现c语言--03
实现 mystrcpy(), mystrcmp(), mystrcat(), mystrlen() ; #include<stdio.h> #include<stdlib.h> ...
- FFmpeg常用命令 (三)流媒体
前言 如此强大的FFmpeg,能够实现视频采集.视频格式转化.视频截图.视频添加水印.视频切片.视频录制.视频推流.更改音视频参数功能等.通过终端命令如何实现这些功能,Richy在本文做一记录,以备之 ...
- 21.java方法详解
public class MethondTest07{ //入口 public static void main(String[] args){ A.m1(); //error:若方法名字的前面什么都 ...
- Android实例1:button点击响应
个人网站http://www.ravedonut.com/ Layout xml文件 <RelativeLayout android:layout_width="wrap_conten ...
- java调用POI读取Excel
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls:XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx: 1.注意点 getPhy ...