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 ifs2 is a scrambled string of s1.

Have you met this question in a real interview?

Yes
Example

 
Challenge

O(n3) time

LeetCode上的原题,请参见我之前的博客Scramble String

解法一:

class Solution {
public:
/**
* @param s1 A string
* @param s2 Another string
* @return whether s2 is a scrambled string of s1
*/
bool isScramble(string& s1, string& s2) {
if (s1 == s2) return true;
if (s1.size() != s2.size()) return false;
string t1 = s1, t2 = s2;
sort(t1.begin(), t1.end());
sort(t2.begin(), t2.end());
if (t1 != t2) return false;
int n = s1.size();
for (int i = ; i < s1.size(); ++i) {
string a1 = s1.substr(, i), b1 = s1.substr(i), a2 = s2.substr(, i), b2 = s2.substr(i);
string a3 = s2.substr(n - i), b3 = s2.substr(, n - i);
if ((isScramble(a1, a2) && isScramble(b1, b2)) || (isScramble(a1, a3) && isScramble(b1, b3))) {
return true;
}
}
return false;
}
};

解法二:

class Solution {
public:
/**
* @param s1 A string
* @param s2 Another string
* @return whether s2 is a scrambled string of s1
*/
bool isScramble(string& s1, string& s2) {
if (s1 == s2) return true;
if (s1.size() != s2.size()) return false;
int n = s1.size();
vector<vector<vector<bool>>> dp(n, vector<vector<bool>>(n, vector<bool>(n + , false)));
for (int i = n - ; i >= ; --i) {
for (int j = n - ; j >= ; --j) {
for (int k = ; k <= n - max(i, j); ++k) {
if (s1.substr(i, k) == s2.substr(j, k)) {
dp[i][j][k] = true;
} else {
for (int t = ; t < k; ++t) {
if ((dp[i][j][t] && dp[i + t][j + t][k - t]) || (dp[i][j + k - t][t] && dp[i + t][j][k - t])) {
dp[i][j][k] = true;
break;
}
}
}
}
}
}
return dp[][][n];
}
};

解法三:

class Solution {
public:
/**
* @param s1 A string
* @param s2 Another string
* @return whether s2 is a scrambled string of s1
*/
bool isScramble(string& s1, string& s2) {
if (s1 == s2) return true;
if (s1.size() != s2.size()) return false;
int n = s1.size(), m[] = {};
for (int i = ; i < n; ++i) {
++m[s1[i] - 'a'];
--m[s2[i] - 'a'];
}
for (int i = ; i < ; ++i) {
if (m[i] != ) return false;
}
for (int i = ; i < n; ++i) {
string a1 = s1.substr(, i), b1 = s1.substr(i);
string a2 = s2.substr(, i), b2 = s2.substr(i), a3 = s2.substr(n - i), b3 = s2.substr(, n - i);
if ((isScramble(a1, a2) && isScramble(b1, b2)) || (isScramble(a1, a3) && isScramble(b1, b3))) {
return true;
}
}
return false;
}
};

[LintCode] Scramble String 爬行字符串的更多相关文章

  1. [LeetCode] 87. Scramble String 爬行字符串

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

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

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

  3. [LeetCode] 87. Scramble String 搅乱字符串

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

  4. 087 Scramble String 扰乱字符串

    给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树.下图是字符串s1 = "great"的一种可能的表示形式.    great   /    \ ...

  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 乱串

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

  7. 45. Scramble String

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

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

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

  9. Leetcode:Scramble String 解题报告

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

随机推荐

  1. servlet、genericservlet、httpservlet之间的区别

    转自:http://blog.csdn.net/rat9912345/article/details/5161789 当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法 ...

  2. [荐]javascript Date format(js日期格式化)

    cnblog:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html 方法一: // 对Date的扩展,将 Date  ...

  3. 命令模式/command模式/行为型模式

    举个栗子 指挥官向士兵下达命令,士兵执行 实现代码如下: class Soldier { public void exe() { System.out.println("执行命令" ...

  4. World of Warcraft

    一大堆快捷键 鼠标中键:致盲 上滚:闪避 下滚:疾跑 通用:~伺机待发 Q加速 E斗篷 R徽章 T消失 alt+Q脚踢,alt+W毒刃 alt+E亡灵意志 alt+D烟幕弹 alt+F死亡标记 Z天降 ...

  5. 轻松搞定javascript预解析机制(搞定后,一切有关变态面试题都是浮云~~)

    hey,guys!我们一起总结一下JS预解析吧! 首先,我们得搞清楚JS预解析和JS逐行执行的关系.其实它们两并不冲突,一个例子轻松理解它们的关系: 你去酒店吃饭,吃饭前你得看下菜谱,点下菜(JS预解 ...

  6. 【转】Kylin的Hierarchies,Derived维度方面配置优化

    http://blog.csdn.net/jiangshouzhuang/article/details/51286150 Hierarchies: 理论上对于N维度,我们可以进行2的N次方的维度组合 ...

  7. CSS3-给网页添加图片

    给网页添加图片: 1.background-attachment: scroll--------随文本一块滚动 ; background-attachment: fixed-----固定在一个位置上 ...

  8. Gym100685G Gadget Hackwrench(倍增LCA)

    题目大概说一棵边有方向的树,q个询问,每次询问结点u是否能走到v. 倍增LCA搞即可: 除了par[k][u]表示u结点往上走2k步到达的结点, 再加上upp[k][u]表示u结点往上走2k步经过边的 ...

  9. checkbox属性checked="checked"通过js已设置,但是不勾选

    1.通过 attr('checked','checked') 来设置checkbox时,重复点击,虽然checked属性设置正确,但是checkbox没有被勾选 ,如下代码:(代码是全选功能) $(' ...

  10. dom事件不求甚解,色解事件捕获和冒泡

    以前对事件只会用jq的bind绑定一下,脑海里留着书中的事件循环,一直认为事件就是这儿循环的,最近看园子里的文章,对事件的了解更模糊了 所以我做了个小实验,总结一下看的这些零零碎碎的文章,如果总结错了 ...