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 ...
随机推荐
- servlet理论学习
servlet是和凭条无关的服务器端的组件,它运行在servlet容器中,servlet容器负责servlet和客户的通信以及调用servlet方法.servlet和客户的通信是采用“请求和响应的模式 ...
- hdu 6058
\(f(l,r,k)=\)区间[\(l\),\(r\)]的第k大. \(\sum_{l=1}^{n}{\sum_{r=l}^{n}{f(l,r,k)}}\) 参考题解,claris大佬题解.赛后AC. ...
- 【CQ18高一暑假前挑战赛3.5】标程
[A:快速幂相关] #include<bits/stdc++.h> using namespace std; int qpow(int a,int x){ a%=;; while(x){ ...
- POJ1201 Intervals (差分约束)
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: ...
- 无向图hash
一个效果还行的 无向图hash判同构的方法 求出每个点向其它点的最短路,然后排序,然后按字符串拼接起来,再按每个点的字符串 排序后的rank 作为每一个点的初始hash值 然后每一轮,把每个点的相邻点 ...
- hibernate 中的拦截器EmptyInterceptor接口功能
Interceptor接口提供了从会话(session)回调(callback)应用程序(application)的机制, 这种回调机制可以允许应用程序在持久化对象被保存.更新.删除或是加载之前,检查 ...
- 微信公众平台:微信JS-SDK Demo
ylbtech-微信公众平台:微信JS-SDK Demo 1. HTML返回顶部 1.demo.html <!DOCTYPE html> <html> <head> ...
- bzoj4004
线性基 构成线性基的个数是定的,所以我们对价值进行贪心就行了,根据拟阵那套理论,我们排个序,然后能塞进去就塞,这样就求出最小值了. 思维江化,只要是多维向量都能用线性基搞. #include<b ...
- PHP开发api接口 -- 安全验证 生成签名
转载博客 ————. http://blog.csdn.net/li741350149/article/details/62887524 REST模式中HTTP请求方法(GET,POST,PUT,DE ...
- 20个Flutter实例视频教程-第12节: 流式布局 模拟添加照片效果
视频地址: https://www.bilibili.com/video/av39709290/?p=12 博客地址: https://jspang.com/post/flutterDemo.html ...