leetcode87. Scramble String

题意:

给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树。

思路:

递归解法

对于每对s1,s2. 在s1某处切一刀,s1分成left,right,然后在s2首部开始等长的地方切一刀,切成left,right.只要s1的left和s2的left,s1的right和s2的right同样也构成scramble string就可以了。 递归解法需要剪枝。

  • 剪枝1:s1,s2不等长return
  • 剪枝2:s1,s2字符不相同return

动态规划

这题动态规划解法时间不如递归的,但是这题用动态规划还是比较牛皮的。map[i][j][k]三维数组储存,i,j分别表示s1,s2开始的下标,k表示i,j开始后组成的长度。虽然说看到两个字符串比较感觉像动态规划。但是不好想呀。

参考博文

ac代码:

C++ (递归)

class Solution {
public:
bool isScramble(string s1, string s2) { int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(len1 == 1) return s1[0] == s2[0];
int cnt[256] = {0};
for(int i = 0; i < len1; i++) cnt[s1[i]]++;
for(int j = 0; j < len2; j++) cnt[s2[j]]--;
for(int i = 0; i < 256; i++)
if(cnt[i]) return false; for(int i = 0; i < len1 - 1; i++)
{
if(isScramble(s1.substr(0,i + 1),s2.substr(0,i + 1)) && isScramble(s1.substr(i + 1),s2.substr(i + 1)) ||
isScramble(s1.substr(0,i + 1),s2.substr(len2 - 1 - i, i + 1)) && isScramble(s1.substr(i + 1),s2.substr(0,len2 - 1 - i)))
return true;
}
return false;
}
};

C++ (动态规划)

class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(!len1) return true;
vector<vector<vector<bool> > >map(len1, vector<vector<bool> >(len1, vector<bool>(len1, false))); for(int j = 0; j < len1; j++)
for(int k = 0; k < len1; k++)
if(s1[j] == s2[k]) map[0][j][k] = true; for(int i = 1; i < len1; i++)
{
for(int j = len1 - 1; j >= 0; j--)
{
for(int k = len1 - 1; k >= 0; k--)
{
if(k + i + 1 > len1 || j + i + 1 > len1) continue;
//map[i][j][k] = false;
for(int u = 0; u < i; u++)
{
if((map[u][j][k + i - u] && map[i - u - 1][j + u + 1][k]) ||
(map[u][j][k] && map[i - u - 1][j + u + 1][k + u + 1]))
{
map[i][j][k] = true;
break;
}
}
}
}
}
return map[len1 - 1][0][0];
}
};

leetcode87. Scramble String的更多相关文章

  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. [LintCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. 45. Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  4. 【LeetCode练习题】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  5. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  8. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  9. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

随机推荐

  1. webgote的例子 数据库与sql注入的相关联系(1)

    大家好我是时光凉春衫薄 之前将讲的sql注入有点随便了我同事也觉得有些地方看不懂,往后的几天我尽量写的细一点.尽可能让大家能看懂.(新手出道大佬多多指教.欢迎评论批评.) 数据库与sql注入的相关联系 ...

  2. device tree --- #address-cells and #size-cells property

    device tree source Example1 / { #address-cells = <0x1>; // 在 root node 下使用 1 個 u32 來代表 address ...

  3. memcached安装【转】

    1.安装依赖软件 # yum -y install libevent libevent-devel perl-Test-Harness perl-Time-HiRes perl-TermReadKey ...

  4. Dubbo使用

    [注:本文参考<Dubbo入门---搭建一个最简单的Demo框架>,感谢原创作者的知识探索与奉献] 一.Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起.  ...

  5. LINUX gcc安装rpm包顺序

    rpm -ivh cpp-4.1.2-42.el5.i386.rpm rpm -ihv kernel-headers-2.6.18-92.el5.i386.rpm rpm -ivh glibc-hea ...

  6. 3.rabbitmq 发布/订阅

    1. 发布者 #coding:utf8 import pika import json import sys message = ''.join(sys.argv[1:]) or "hell ...

  7. Ecshop二次开发修改

    所有的错误,根据下面这个帖子修改 http://bg.artuion.com/328.html 修改错误: 第300行 原有内容: //return preg_replace("/{([^\ ...

  8. HTTP协议头注射漏洞实例

    HTTP 响应头文件中包含未经验证的数据会引发 cache-poisoning.cross-site scripting.cross-user defacement.page hijacking.co ...

  9. JAVA(一)变量

    public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" ...

  10. Codeigniter处理用户登录验证后URL跳转

    涉及到My_Controller.php以及登录验证模块User.php,代码如下: My_Controller.php class MY_Controller extends CI_Controll ...