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. 简单json语句转化为map保存

    主要用到了 net.sf.json.JSONObject类 需要用到的jar包 : jar包下载地址 package test; import java.io.BufferedReader; impo ...

  2. 第十章:Intent详解

    [正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用 ...

  3. github的注册过程

    带着疑问打开了github.这是一个神奇的网站,因为它到处都是英语,对于我这种英语盲这简直太痛苦了.借助了百度翻译,我还是马马虎虎的完成了github的制作. 首先在它的登录界面下面有一个sign u ...

  4. SQL Server 2005 控制用户权限访问表

    转自:http://www.cnblogs.com/gaizai/archive/2011/07/14/2106617.html 一.需求 在管理数据库过程中,我们经常需要控制某个用户访问数据库的权限 ...

  5. PHP中的null合并运算符

    project: blog target: null-coalesce-operator-in-php.md date: 2015-12-30 status: publish tags: - Null ...

  6. flask开发遇到 Must provide secret_key to use csrf解决办法

    开发flask的时候,遇到了 Must provide secret_key to use csrf错误提醒.原来是没有设置secret_key .在代码中加上 app.config['SECRET_ ...

  7. 项目中如果管理前端文件CSS和JS

    如何管理CSS和JS文件,一直是前端一个热门的话题.下面将简单分享一下使用心得,欢迎大家吐槽.拍砖和提供更好的实现方式. 一.管理CSS文件,本博客将讨论less管理. iReset.less.iBu ...

  8. php部分---一个分页类、用法

    1.分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...

  9. java.lang.Enum<E extends Enum<E>>

    public enum Direction { L, LU, U, RU, R, RD, D, LD, STOP, JUMP;} for(Direction d: Direction.values() ...

  10. kuangbin_SegTree A (HDU 1166)

    大牛们的文章里这句 题意:O(-1) 思路:O(-1) 深深地嘲讽了我........ 不过单点更新 区间求和也算是基本操作了吧 (虽然我还是看了好久才理解) 跟之前学图论的时候感觉完全不一样啊orz ...