[Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great":
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that "rgeat" is a scrambled string of "great".
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a
We say that "rgtae" is a scrambled string of "great".
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
Solution:
public class Solution {
public boolean isScramble(String s1, String s2) {
if(s1.equals(s2))
return true;
int l1=s1.length();
int l2=s2.length();
if(l1!=l2)
return false;
if(l1==0)
return true;
if(l1==1)
return s1.equals(s2);
char[] c_s1=s1.toCharArray();
char[] c_s2=s2.toCharArray();
Arrays.sort(c_s1);
Arrays.sort(c_s2);
for(int i=0;i<c_s1.length;++i){
if(c_s1[i]!=c_s2[i])
return false;
}
boolean b=false;
for(int i=1;i<l1&&!b;++i){
String s11=s1.substring(0,i);
String s12=s1.substring(i);
String s21=s2.substring(0,i);
String s22=s2.substring(i);
b=isScramble(s11, s21)&&isScramble(s12, s22);
if(!b){
String s31=s2.substring(0, l1-i);
String s32=s2.substring(l1-i);
b=isScramble(s11, s32)&&isScramble(s12, s31);
}
}
return b;
}
}
------------------------------------------------------------------------------------
20150220:
public class Solution {
public boolean isScramble(String s1, String s2) {
if(s1==null||s2==null||s1.length()!=s2.length())
return false;
if(s1.equals(s2))
return true;
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
for(int i=0;i<c1.length;++i){
if(c1[i]!=c2[i])
return false;
}
int N=s1.length();
for(int i=1;i<N;++i){
String s1_temp1=s1.substring(0,i);
String s1_temp2=s1.substring(i);
String s2_temp1=s2.substring(0,i);
String s2_temp2=s2.substring(i);
if(isScramble(s1_temp1, s2_temp1)&&isScramble(s1_temp2, s2_temp2))
return true;
String s2_temp3=s2.substring(0,N-i);
String s2_temp4=s2.substring(N-i);
if(isScramble(s1_temp1, s2_temp4)&&isScramble(s1_temp2, s2_temp3))
return true;
}
return false;
}
}
[Leetcode] Scramble String的更多相关文章
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [LeetCode] 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] Scramble String 字符串 dp
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- CSS3实现32种基本图形
CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出.直接用CSS3画出这些图形,要比贴图性能更好,体验更加,是一种非常好的网页美观方式. 这32种图形分别为圆形,椭圆形,三角形,倒三角形, ...
- devices-list
转自:https://www.kernel.org/pub/linux/docs/lanana/device-list/devices-2.6.txt LINUX ALLOCATED DEVICES ...
- Pyqt 窗体间传值
窗体间传值网上有好多方法,比如新建文件,先将子类窗体的数据传到文件中,父窗体读取文件. Signal&Slot机制进行传值 等等 在这里,我们就举个采用apply方法:Signal& ...
- sqlplus使用(二)
详见SQL*Plus® User's Guide and Reference Release 11.2 5 Using Scripts in SQL*Plus 1.定义环境变量 _EDITOR ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- C#路径/文件/目录/I/O常见操作汇总
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- Nginx+lua环境搭建
其实有点类似WampServer一站式安装包 wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz tar -zxvf ng ...
- C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏
1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...
- [Outlook]设置邮件自动接收时间
[Outlook]设置邮件自动接收时间 找了好久,一直都没设置正常,导致老是收到邮件有延迟,今天头脑清晰,搜了一下,然后自己竟然给找到了,记下来当笔记,好记性不如烂笔头,呵呵 搜索百度&quo ...
- 在Virtulbox上装Ubuntu
做个程序员,会用Linux,这应该是最基本的要求吧.可惜本人经常用Windows,只是偶尔去服务器上做些操作的时候才接触到linux.so,我要学Linux.刚学所以还是先装个虚拟机吧,等在虚拟机上用 ...