一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:判断两个字符串是不是Scramble String,题目有点长,需要读好久!!!

举个简单的例子说明Scramble String,如ab和ba,那么ab拆分成a和b,ba拆分成b和a,这样就代表为Scramble String。

那么s1和s2是否为Scramble String,就需要将s1和s2拆分成两部分s11和s12,s21和s22,需要判断这两部分是否为Scramble String。

可以分成两个部分:(s11和s21,s12和s22)以及(s11和s22,s12和s21)。

这样一来代码就比较好写了!

很明显的动态规划问题。但是本人在做题过程中的优化还是没有做好,导致超时。

class Solution {
public:
    bool isScramble(string s1, string s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        if (len1 != len2) return false;//长度不一样
        if (s1 == s2) return true;//子串相等

        //判断两个子串包含的字符是否相同
        //最开始没加这一步,导致一直超时
        vector<int> count(26,0);
        for(int i = 0 ; i < len1 ; i++)
        {
            count[s1[i]-'a']++;
            count[s2[i]-'a']--;
        }
        for(int i = 0 ; i < 26 ; i++)
        {
            if(count[i]!=0) return false;
        }
        //递归
        for (int i = 1; i < len1; i++)
        {
            string subs1_1 = s1.substr(0, i);
            string subs1_2 = s1.substr(i);
            string subs2_1 = s2.substr(0, i);
            string subs2_2 = s2.substr(i);
            bool is1 = (subs1_1==subs2_1?true:isScramble(subs1_1, subs2_1)) && (subs1_2==subs2_2?true:isScramble(subs1_2, subs2_2));//这里也对递归进行了优化,相等则不进行递归
            subs2_1 = s2.substr(len2-i, i);
            subs2_2 = s2.substr(0,len2-i);
            bool is2 = (subs1_1==subs2_1?true:isScramble(subs1_1, subs2_1)) && (subs1_2==subs2_2?true:isScramble(subs1_2, subs2_2));
            if (is1 || is2) return true;//两部分任一部分为Scramble String则s1和s2就为Scramble String
        }
        return false;
    }
};

【一天一道LeetCode】#87. Scramble String的更多相关文章

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

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

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

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

  3. [LeetCode] 87. Scramble String 搅乱字符串

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

  4. [LeetCode] 87. Scramble String 爬行字符串

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

  5. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  6. leetcode@ [87] Scramble String (Dynamic Programming)

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

  7. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

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

  8. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  9. 【leetcode】Scramble String

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

随机推荐

  1. event工具集

    eventTool = { // 页面加载完成后 readyEvent : function(fn) { if (fn==null) { fn=document; } var oldonload = ...

  2. 离线合成联想到的--canvas合成水印

    前段时间做了功能模块:用户设置自定义勋章: 实现方式:前端把用户设置的昵称传到后台,后台根据不同用户等级,使用离线合成技术合成不同的勋章返回到前端: 方案算是实现了,但是有点坑就是,后台的离线合成没有 ...

  3. 如何上传本地项目到gitHub解决方案

    最近有人有人问到我怎么将新创建的本地代码上传到github上,这里简单的记录一下,我喜欢使用命令行,这里全用命令行来实现,不了解Git命令的可以去了解下. 1.  建立本地仓库,cd到你想要上传文件的 ...

  4. 重写轮子之 ID3

    这是半成品, 已完成了 fit() 部分, 形成了包含一棵完整树的 node 对象. 后续工作是需解析该 node对象, 完成 predict() 工作. # !/usr/bin/python # - ...

  5. 关于熊猫认证软件IOS安装步骤教程(适用于其他软件)

    IOS运行企业版应用教程 1.扫描二维码之后微信进入界面,如下图所示:点击右上角三个点   2.弹出分享界面,如图所示:点击苹果自带浏览器(sarfari)     3.进入苹果自带浏览器后如图所示, ...

  6. 使用 Nexus Repository Manager 搭建私有docker仓库

    使用容器安装Nexus3 1.下载nexus3的镜像: docker pull sonatype/nexus3 2.使用镜像启动一个容器: docker run -d --name nexus  -- ...

  7. Docker标准化开发测试和生产环境

    对于大部分企业来说,搭建 PaaS 既没有那个精力,也没那个必要,用 Docker 做个人的 sandbox 用处又小了点. 可以用 Docker 来标准化开发.测试.生产环境. Docker 占用资 ...

  8. 【Java 语言】Java 多线程 一 ( 线程启动 | 线程中断 )

    一. 线程启动 线程启动 : -- 1. 继承 Thread 运行线程 : 重写 Thread 类的 run 方法, 然后执行该线程; -- 2. 实现 Runnable 接口, 并运行线程; -- ...

  9. Bootstrap3 表格-鼠标悬停

    通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应. <table class="table table-hover" ...

  10. Dynamics CRM 本地插件注册器连CRMAn unsecured or incorrectly secured fault was received from the other party

    今天遇到个问题,在本地打开插件注册器连接到远程CRM服务器时报如下问题 但我在CRM服务器上连接注册器是可以打开的,所以不存在账号权限这类的问题了(当然我用的是超管的账号也不可能存在),最后查询得知是 ...