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. [CQOI 2014] 数三角形 & 机械排序臂

    数三角形 bzoj 3505 要知道一个公式就是(a,b)和(x,y)两点所成线段上面的整点数是gcd(a-x,b-y)-1,通过枚举原点到map上任意一点所能成的三角形,再平移,得到要去掉的三点共线 ...

  2. androidd 程序默认安装位置和数据存储位置(公用和私用)

    默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...

  3. How to browse the entire documentation using XCode 5 Documentation and API Reference ?

    file:///Users/yangiori/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.Ap ...

  4. Oracle数据库初级学习 2

    今天我们介绍Oracle数据库中剩余的查询方法,今天的查询方法会比昨天的更为复杂一些(PS:我也是个初学者,请见谅..). 一.分组函数 分组函数是为了区分同一个表中的不同数据而建立,其关键字为GRO ...

  5. tensorflow3

    参考文献:tensorflow_manual_cn.pdf 一.tensorflow和caffe对应: graph-->.prototxt定义的网络结构 session-->solver( ...

  6. H5版俄罗斯方块(3)---游戏的AI算法

    前言: 算是"long long ago"的事了, 某著名互联网公司在我校举行了一次"lengend code"的比赛, 其中有一题就是"智能俄罗斯方 ...

  7. UE4 VR 模式下全屏解决办法

    方法步骤: 1.打开关卡蓝图添加如下代码: 2.设置配置文件在工程目录里面找到 Config 文件夹在里面添加一个配置文件并命名为 DefaultGameUserSettings.ini 把如下内容贴 ...

  8. IOS 不兼容 伪类active

    添加空的 事件ontouchstart 例 <body ontouchstart>

  9. 跨域之-jquery操作

    在JQ进行跨域的操作,用的是jsonp的方式,创建script标签,除了跨域的行为外,本地的操作方式都是xmlHttpRequest.

  10. 多媒体音频(audio)

    随着计算机技术的发展,特别是海量存储设备和大容量内存在PC机上的实现,对音频媒体进行数字化处理便成为可能.数字化处理的核心是对音频信息的采样,通过对采集到的样本进行加工,达成各种效果,这是音频媒体数字 ...