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. ACM_1 大数求和

    /*1 *2014.11.18 *大数求和 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <strin ...

  2. 如何用SQL命令行工具删除dedecms指定id文章

    用dedecms采集时标题字段设置错了,出现了注释符号<!---->,导致后台的文章列表出现错误,也无法直接从列表中删除,可以远程登录数据库去操作,这个相对比较麻烦,想着直接从后台的SQL ...

  3. php 通过curl post发送json数据实例

    例1  代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...

  4. Windows下几个常用的和进程有关的命令

    在windows下,进入cmd,有几个常用的和进程有关的命令: netstat -ano:查看所有进程 netstat -ano|findstr  “端口号”:查看端口号对应的进程PID taskli ...

  5. springmvc之前后台传值

    一.向后台传值 1.项目结构 2.jar包 3.spring-config.xml <?xml version="1.0" encoding="UTF-8" ...

  6. Java设计模式 之 工厂方法模式

    1. 使用设计模式的好处:可提高代码的重复性,让代码更容易被他人理解,保证代码的可靠性. 2. 工厂模式定义:就是创建一个工厂类来创建你需要的类,工厂模式包括工厂模式和抽象工厂模式,抽象工厂模式是工厂 ...

  7. js图文讲解

       

  8. Google在三大系统上停止对Chrome Apps的支持

    近年来凭借着低廉的价格和易于管理和追踪的特性,Chrome OS设备逐渐获得了市场的肯定.只是相比较Windows和macOS桌面系统来说,Chrome OS在应用方面依然存在劣势,为此三年前Goog ...

  9. JavaScript 与函数式编程

    原文:https://bethallchurch.github.io/JavaScript-and-Functional-Programming/ 译文:http://www.zcfy.cc/arti ...

  10. [转载]html中DTD使用小结

    原文链接:http://www.jb51.net/web/36856.html DTD 是一套关于标记符的语法规则.它是XML1.0版规格得一部分,是html文件的验证机制,属于html文件组成的一部 ...