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 ...
随机推荐
- struts2 学习日记1
struts2 简介 struts2的前身可以说是framework.strut1作为当时很流行的框架,但是有很多的不足之处,framework出生后,它带来了很好的框架,但是很多人已经习惯了stru ...
- tensorflow sigmoid_cross_entropy_with_logits 函数解释
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None) sigmoid_ ...
- Log4j输出格式控制--log4j的PatternLayout参数含义
参数 说明 例子 %c 列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间 log4j配置文件参数举例 输出显示媒介 假设当前logger名字 ...
- 简单数位DP
https://cn.vjudge.net/problem/HDU-4722 懒得写看,代码注释吧;主要存板子 #include <cstdio> #include <cstring ...
- poj-3666
http://vjudge.net/problem/POJ-3666 题目是dp 题目; 简单dp 离散一下就好. 我们先来讲一讲不离散的,简单的懂了,其他的也很容易. dp[i] 代表这个数列以 ...
- vmware 克隆linux虚拟机后的网卡修改方法
VMware虚拟机安装好CentOS6.5系统后,纯净的系统多克隆几份出来方便后期做试验.克隆步骤很简单,克隆后出现的问题是克隆后的网卡MAC地址和原系统MAC地址一样,在局域网内会有冲突,需要更改克 ...
- 合并多个cv::Mat类型,即合并多个图片的接口
1. cv::Mat get_merage_image(cv::Mat cur_frame) { cv::Mat image_one=cur_frame; cv::Mat image_two=cur_ ...
- bzoj 2151 种树——贪心+后悔
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2151 似乎是半年+前讲过的.(然而看到的时候却不会了) 考虑贪心,限制就是不能选两边的.如果 ...
- JavaScript-Tool:jquery.qrcode.js
ylbtech-JavaScript-Tool:jquery.qrcode.js 1.返回顶部 1. 插件描述:jquery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jqu ...
- css样式 -- 表格不会因为字体过长导致字体溢出的问题
常常碰到因为表格大小就麽大了,字体过长会爆炸溢出的问题,我们后端就用这个可以了,溢出的可以省略号 ... 代替好了. /* 在表格css样式加上这三个就可以了 效果就会变成 “abc...” */ { ...