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.

注:Google 面试题。

分析: 从上往下考虑,比然存在一个位置,使得两个字符串分成相同的两个子串,他们位置前后相同或者前后相反。如此递归即可。能递归也就能动态规划记住子问题的解。但是本题没有使用动态规划也很快AC.

bool hasSameAlpha(string &s1, string &s2) {
int v = 0;
for(int i = 0; i < s1.size(); ++i) v ^= (s1[i] ^ s2[i]);
return v == 0;
}
class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1.size() != s2.size()) return false;
if(s1 == s2) return true;
if(!hasSameAlpha(s1, s2)) return false;
int n = s1.size();
for(int i = 1; i < n; ++i) {
string s11 = s1.substr(0, i);
string s12 = s1.substr(i);
string s21 = s2.substr(0, n-i);
string s22 = s2.substr(n-i);
if((isScramble(s11, s2.substr(0, i)) && isScramble(s12, s2.substr(i))) || (isScramble(s11, s22) && isScramble(s12, s21)))
return true;
}
return false;
}
};

45. Scramble String的更多相关文章

  1. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [LintCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. 【LeetCode练习题】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  4. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  7. leetcode87. Scramble String

    leetcode87. Scramble String 题意: 给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树. 思路: 递归解法 对于每对s1,s2. 在s1某处切一刀,s ...

  8. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  9. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

随机推荐

  1. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  2. linux命令每日一练习-rmdir mv

    rmdir 删除一个空的文件夹,当目标文件夹非空的时候不能删除 mv 移动文件或目录 mv source target mv -i *** *** 如果目标文件存在则询问是否覆盖 mv -f *** ...

  3. JVM-并发-线程

    线程 1.线程的实现 (1)实现线程主要有3中方式:使用内核线程实现,使用用户线程实现和使用用户线程加轻量级进程混合实现. (2)使用内核线程实现 内核线程就是直接由操作系统内核支持的线程,这种线程由 ...

  4. C语言程序设计第一次作业

    同学们,我们已经留了两次实验了,请大家将这两次的实验课内容写成实验报告在截止日期前进行提交. 截止日期:2016-10-7 23:00 实验一: 编程打印5行的倒三角形,第一行打印9个*,第二行7个* ...

  5. Gradle for Android

    1.project vs project.rootProject 2.System.console() != null ? System.console().readPassword("\n ...

  6. 【Android】配置APK开发环境

    1.安装java jdk去oracle公司下载jdk-7u15-windows-i586.exehttp://www.oracle.com/technetwork/cn/java/javase/dow ...

  7. BZOJ 1433 二分图上的博弈

    首先对网格染色,发现是而二分图. 那么即在二分图上选一个起点走过的点无法再走,最后无路可走就输了. 如果起点必在最大匹配中,先手必赢. 如果起点不一定在最大匹配中(包括不可能在),后手必赢.网上有解释 ...

  8. LintCode Search Insert Position

    找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * para ...

  9. 2015GitWebRTC编译实录11

    2015.07.21 ilbc 编译通过注意有几个win32打头的文件,其实都是要编进去的[429/1600 ] CC obj ilbc.abs_quant.o[430/1600 ] CXX obj ...

  10. 初次学习c语言

    #include<stdio.h> main(){      int o,p,q; scanf("%d%d",&o,&p); q=o+p; printf ...