【leetcode】 Scramble String (hard)★
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)★的更多相关文章
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
随机推荐
- FireBug调试工具笔记
Firebug是网页浏览器 Mozilla Firefox下的一款开发类插件, 现属于Firefox的五星级强力推荐插件之一.它集HTML查看和编辑.Javascript控制台.网络状况监视 ...
- Some Delphi tips
====================================conversion routines====================================Format('T ...
- 【转载】利用Unity自带的合图切割功能将合图切割成子图
虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...
- git 初始化
Git global setup git config --global user.name "杨清1" git config --global user.email " ...
- R语言 推荐算法 recommenderlab包
recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...
- 在Mac mini上编译Android源码
参考文章 1.Android 6.0 源代码编译实践 2.编译Android源码致命错误解决方案 实践过程 1.Mac下安装Ubuntu双系统 (1)Ubuntu版本:Ubuntu 15.10 注:实 ...
- Sublime Text 2 安装emmet插件和常用快捷键
一.先安装package control1.按Ctrl+`调出console,输入以下命令然后回车 import urllib2,os; pf='Package Control.sublime-pac ...
- CHAP算法C++实现
CHAP是一种挑战响应式协议. CHAP全称为:Challenge Handshake Authentication Protocol. CHAP密码 = 一个字节的标识符 + MD5(一个字节的标识 ...
- MongoDB的索引(四)
创建索引的好处是可以加快查询速度,但是但来的负面影响就是磁盘的开销和降低写入性嫩. 查看评判当前索引构建情况方法: 1. 使用mongostat工具: 查看mongodb运行状态的程序 使用格式:mo ...
- DOM之操作技术
1.1 动态脚本 动态加载的外部JS文件能够立即运行.难点在于如何知道脚本加载完成了?可以通过事件来检测.IE对待<script>元素特殊性,不允许DOM访问其子节点.使用元素的text属 ...