[LintCode] 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 ifs2
is a scrambled string of s1
.
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 爬行字符串的更多相关文章
- [LeetCode] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 087 Scramble String 扰乱字符串
给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树.下图是字符串s1 = "great"的一种可能的表示形式. great / \ ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 45. Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
随机推荐
- 遍历Map
Map map = new HashMap(); map.put("1", "value1"); map.put("2", "va ...
- loj 1412(树上最长直径的应用)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1412 思路:好久没写题解了,有点手生,这题从昨天晚上wa到现在终于是过了...思想其实 ...
- 面向服务的架构SOA
SOA简介 SCA实现SOA的最佳方式 Apache开源框架Tuscany实现SCA架构 SOA简单描述: SOA(Service-Oriented Architecture)面向服务的体系架构.为了 ...
- LoadRunner Pacing设置(转)
转载的,备读 在 LoadRunner 的运行场景中,有一个不大起眼的设置,可能经常会被很多人忽略,它就是Pacing .具体设置方式为: Run-Time settings à General à ...
- RecyclerView 介绍 02 – 重要概念
几个概念 RecyclerView是一个ViewGroup: LayoutManager控制RecyclerView的ChildView的布局显示,childview由Recycler提供以及管理: ...
- iOS10 UI教程基础窗口的内容与设置起始窗口
iOS10 UI教程基础窗口的内容与设置起始窗口 iOS10 UI教程基础窗口的内容与设置起始窗口,本章我们从iOS10开发中UI的基础知识开始讲解,其中包括了窗口.视图以及UI层次结构和Views的 ...
- 【noip2014T3】
上文有提到noip2014还有没A的嘛..就先把这个坑给填了 flappy bird好sad啊 还是先做解方程 八中的数据好强了,然而我最后凑了四个质数就A了,感谢shy! 作为联赛最后一题,学习它的 ...
- Java线程并发控制基础知识
微博上众神推荐今年4月刚刚出版的一本书,淘宝华黎撰写的<大型网站系统与Java中间件实践>,一线工程师的作品,实践出真知,果断要看. 前两章与<淘宝技术这十年>内容类似,基本是 ...
- HTML5中createPattern()
定义和用法 createPattern() 方法在指定的方向内重复指定的元素. 元素可以是图片.视频,或者其他 <canvas> 元素. 被重复的元素可用于绘制/填充矩形.圆形或线条等等. ...
- oracle 授权
1.授权oss01用户,此用户可以执行sys.utl_i18n存储过程. grant execute on sys.utl_i18n to oss01;grant execute on sys.dbm ...