87. Scramble String(js)

87. Scramble String

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.

Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true

Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false题意:给定两个字符串,判断这两个字符串是否互为scramble,scramble的定义是将字符串按照二叉树的形式逐级进行字符拆分,直到不能再拆为止,如果两个字符串互为scramble,那么交换两个叶子节点可以将两个字符串相互转换代码如下:
/**
 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 */
var isScramble = function(s1, s2) {
      if(s1.length!=s2.length)    return false;
        if(s1==s2) return true;
        let str1=s1,str2=s2;
        str1=str1.split(‘‘).sort().join(‘‘)
        str2=str2.split(‘‘).sort().join(‘‘)
        if(str1!=str2) return false;

        for(let i=1;i<s1.length;i++){
            let s11=s1.substr(0,i);
            let s12=s1.substr(i);
            let s21=s2.substr(0,i);
            let s22=s2.substr(i);
            if(isScramble(s11,s21) && isScramble(s12,s22)) return true;
            s21=s2.substr(s1.length-i);
            s22=s2.substr(0,s1.length-i);
            if(isScramble(s11,s21) && isScramble(s12,s22)) return true;
        }
        return false;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10667217.html

时间: 2024-11-05 18:50:07

87. Scramble String(js)的相关文章

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

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 a

[LeetCode] Scramble String(树的问题最易用递归)

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 a

03 前端篇(JS)

参考博客:http://www.cnblogs.com/yuanchenqi/articles/5980312.html JavaScript包括三部分: ECMAScript.DOM.BOM Javascript在开发中大多数情况下是基于对象的,也是面向对象的. ECMAScript:语法.类型.语句.关键字.保留字.运算符.对象 JavaScript 引入方式: 直接编写:建议放在 body 的最后 <script> alert("hello div") </sc

(动态规划、递归) leetcode 87. Scramble String

思路:用递归来做感觉比动态规划简单,题目让我们判断s1和s2是否是scramble string,则s1上(从左起计数)有一个长度为 i 的分割点将s1分为s1_l 和 s1_r 两段 分别与 s2的(从左起计数一个长度为i的) s2_l 和 s2_r 互为 scramble string:或者与 s2的(从右起计数一个长度为i的)s2.substr(s-i) 和 s2.substr(0, s-i) 互为 scramble string . 1)C++ 中的 substr(pos, len) 表

JavaScript(JS)之简单介绍

JavaScript(JS)之简单介绍 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape(网景)接收Nombas的理念,(Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript 微软随后模仿在其IE3.0

JavaScript(JS)之Javascript对象

JavaScript(JS)之Javascript对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的 <script language="javascript"> var aa=Number.MAX_VALUE; //利用数字对象获取可

百度静态资源(JS)公共库

     例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps.bdimg.com/libs/classlist/2014.01.31/classList.min.js cookies.js http://apps.bdimg.com/libs/Cookies.js/0.4.0/cookies.min.js dojo http://apps.bdimg.com/l

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

前台强大的图表(js)

http://www.jqplot.com/ 网址,自己下载下例子看一下就明白了. 这个是我之前做的项目中的例子(仅是部分代码): function publicMethod(){                         var plot1 = $.jqplot(chart, [currYear], {                 seriesColors: ["rgb(23, 108, 238)"],                 title: titles,