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.

判断两个字符串s1和s2是否可以通过旋转实现。

思路:开始傻眼了,反应不过来。第二天恍然大悟,用递归,分左右部分,变成子问题。但是最开始我以为划分树只有一种方式,后来发现树可以随意的划分,左子树可以有任意个字符,右子树也是。所以需要循环遍历所有条件。再然后是可能s1的左半部分在s2中位于左半部分 或者是右半部分,所以对这两种情况也要都考虑到。

  循环的时候用了截枝,如果两边的字符有不一致的直接跳过该次循环。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
using namespace std; class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1.length() != s2.length())
return false;
if(s1 == s2)
return true; if(!isEqual(s1 , s2))
{
return false;
} for(int i = ; i < s1.length(); i++)
{
int leftlength = i;
int rightlength = s1.length() - leftlength;
string s1left1 = s1.substr(, leftlength);
string s2left1 = s2.substr(, leftlength);
string s1right1 = s1.substr(leftlength, rightlength);
string s2right1 = s2.substr(leftlength, rightlength); string s1left2 = s1.substr(rightlength, leftlength);
string s1right2 = s1.substr(, rightlength); if(!isEqual(s1left1,s2left1) && !isEqual(s1left2,s2left1))
{
continue;
}
if((isScramble(s1left1, s2left1) && isScramble(s1right1, s2right1)) || (isScramble(s1left2, s2left1) && isScramble(s1right2, s2right1)))
{
return true;
}
}
return false;
} //判断两个字符串的字母组成是否一致
bool isEqual(string s1, string s2)
{
if(s1.length() != s2.length())
return false;
for(int i = ; i < s1.length(); i++)
{
int locate = s2.find(s1[i]);
if(locate == string::npos)
{
return false;
}
else
{
s2.erase(s2.begin() + locate);
}
}
return true;
}
}; int main()
{
Solution s;
string s1 = "oatzzffqpnwcxhejzjsnpmkmzngneo";
string s2 = "acegneonzmkmpnsjzjhxwnpqffzzto";
//string s1 = "gneo";
//string s2 = "gneo";
bool ans = s.isScramble(s1, s2); return ;
}

有大神给出了正宗动态规划的解法,非常精简,要好好学习。 不过这个方法算了所有的情况所以时间比我的要长,差不多150ms, 我的是50ms左右。

dp[i][j][l] means whether s2.substr(j,l) is a scrambled string of s1.substr(i,l) or not.

class Solution {
public:
bool isScramble(string s1, string s2) {
int len=s1.size();
bool dp[][][]={false};
for (int i=len-;i>=;i--)
for (int j=len-;j>=;j--) {
dp[i][j][]=(s1[i]==s2[j]);
for (int l=;i+l<=len && j+l<=len;l++) {
for (int n=;n<l;n++) { //所有划分左右区间的情况
dp[i][j][l]|=dp[i][j][n]&&dp[i+n][j+n][l-n]; //s1的左边对s2的左边
dp[i][j][l]|=dp[i][j+l-n][n]&&dp[i+n][j][l-n];//s1的左边对s2的右边
}
}
}
return dp[][][len];
}
};

【leetcode】 Scramble String (hard)★的更多相关文章

  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. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  5. 【leetcode】 Interleaving String (hard)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

随机推荐

  1. JS URL传递中文参数时出现乱码的处理

    在浏览器中显示的地址是这样的: 但是按F12调试的时候的地址却变化掉了: 这个肯定是是因为浏览器对url路径默认编码了.这个问题是在我们去取值的时候,得到的就是后面那一大串稀奇古怪的东西.得不到我们想 ...

  2. TCP的流模式与UDP的报文模式对比

    1       案例背景 在学习TCP-IP协议详解卷一时,读到介绍TCP协议的部分,发现TCP的首部是没有报文总长度字段的,而在UDP中是有的,对这个问题的思考引出了两者之间的区别. 2    案例 ...

  3. QT文件读写

    /* //文件读取 QFile f("c:\\t.txt"); if(!f.open(QIODevice::WriteOnly | QIODevice::Text)) { qDeb ...

  4. 必须知道的.net(性能条款)

    以dispose的模式来代替finalize方式:非托管资源的清理主要有终止化操作和Dispose模式两种,其中Finalize方式存在执行时间不确定,运行顺序不确定,同时对垃圾回收的性能有极大的损伤 ...

  5. Mac 如何安装Homebrew?

    到Github官网上搜索Homebrew,找到对应的Homebrew后,查看它的安装文档,链接如下: https://github.com/Homebrew/homebrew/blob/master/ ...

  6. js面试题

    前几天在学习js的时候,碰到了这样一道面试题,要求计算出给你一个随机乱敲的一个字符串,要求在其中找出那个字符出现的次数最多,以及出现的个数. 这你有两种方案,请大家仔细阅读,有可能在你将来的面试中会碰 ...

  7. qstring与char*、基本数据类型的转换

    1.qstring转化为char* QString.toStdString.c_str() 2.char*转化为QString str = QString(QLatin1String(mm)); 3. ...

  8. shell简单使用

    最近需要用到shell脚本实现关机保护作用,总结下语法 要点: 1.linux下编写的shell脚本不能在window下编写,否则会出现^M的错误,用window编写保存,在linux用vim打开,每 ...

  9. mongodb windows install &python

    安装mongo http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ 其中修改: echo logpath=C:\mo ...

  10. lvs部署-DR模式

    DR模式 角色 IP地址 备注 LVS负载均衡器 192.168.119.132 VIP:192.168.119.150    ipvsadm http_Real server 192.168.119 ...