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. Ubuntu 上安装 MongoDB

    官方安装文档:https://docs.mongodb.com/manual/installation/ 安装环境: mongodb-linux-x86_64-ubuntu1404-3.2.6.tgz ...

  2. sidt十六进制代码

    00121453 0F010D 40441200 sidt fword ptr ds:[gliu]0012145A 0F014D B0 sidt fword ptr ss:[ebp-0x50]0012 ...

  3. Java学习笔记(八)——封装

    一.封装 1.定义 将类的信息隐藏在类的内部,不允许外部程序直接进行访问,而是通过该类提供的方法来实现对隐藏信息的操作和方法. 2.优点 (1)只能通过规定的方法访问数据 (2)隐藏类的细节,方便修改 ...

  4. Linux学习笔记(14)文件系统管理

    1 回顾分区与系统 (1)分区类型 Linux的分区一般包括主分区,扩展分区和逻辑分区.其中: 主分区最多只能分4个: 扩展分区只能有一个,也算作主分区的一种,即主分区加扩展分区最多有4个.但扩展分区 ...

  5. LoadRunner替换字符串(可以同时替换多个)

    在global.h中添加代码 /* * @param char* dest 目标串,也就是替换后的新串 * @param const char* src 源字符串,被替换的字符串 * @param c ...

  6. SQL.WITH AS.公用表表达式(CTE)(转)

    一.WITH AS的含义    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...

  7. mac/linux常用命令

    文件 创建文件: touch filename 创建目录: mkdir dirname, 创建目录及文件: mkdir -p dir/file 删除文件/目录: rm [-rf] filename 显 ...

  8. 门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统

    门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统的详细介绍 一. 以PDA等移动终端为媒介,随时随地掌握门店信息. 二. 后台集成了数据统计.多指标分析.销售.库存.会员管理 ...

  9. 个人js类库mycool

    // JavaScript Document Sunbye 1.0 //getElementById //function start var $=function(_id){return docum ...

  10. A - The Moronic Cowmpouter

    Description Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it ...