Nodejs String Sub String substrings(min,max)

Here you can find the source of substrings(min,max)

Method Source Code

/**/*from w  ww  .  ja  v a 2  s .co m*/
 * this algorithm has avg case complexity O(n^2)
 * not so good... make sure you use min and max to cap it
 *
 * TODO: implement a suffix trie
 */
String.prototype.substrings = function(min,max) {
  var ret = [], shrinkingMax;
  min = [min || 1, 1].max();
  max = [max || this.length,this.length].min();
  for (var cursorStart=0,l=this.length; cursorStart<l; cursorStart++) {
    shrinkingMax = [max,this.length - cursorStart].min();
    for (var substrLen=min; substrLen<=shrinkingMax; substrLen++ ) {
      var cursorEnd = substrLen + cursorStart;
      ret.push(this.substring(cursorStart,cursorEnd));
    }
  }
  return ret;
};

Related

  1. substrings()
    String.prototype.substrings = function () {
      var substrings = [];
      for (var i = 0; i < this.length; i++) {
        for (var j = i + 1; j <= this.length; j++) {
          var sub = this.slice(i,j);
          if (substrings.indexOf(sub) === -1 ) {
            substrings.push(sub);
      return substrings;
    };
    console.log("cat".substrings());
    
  2. substrings()
    String.prototype.substrings = function () {
      results = []
      for (i = 0; i < this.length; i++ ) {
        for (j = i + 1; j <= this.length; j++ ) {
          var cur_substr = this.substring(i, j);
          var includes = false;
          for (k = 0; k < results.length; k++ ) {
            if (results[k] === cur_substr) {
              includes = true;
    ...
    
  3. substrings()
    String.prototype.substrings = function() {
      var substrings = [];
      for (var i = 0; i < this.length; i++){
        for (var j = i; j < this.length; j++){
          substrings.push(this.slice(i,j+1));
        };
      };
      return substrings;
    console.log(("cat").substrings());
    
  4. substrings()
    String.prototype.substrings = function () {
      let array = [];
      for(let i=0; i < this.length; i++) {
        for(let j=i + 1; j <= this.length; j++) {
          if (!array.includes(this.slice(i,j))) {
            array.push(this.slice(i,j));
      return array;
    };
    console.log([7,6,5,4,1,3,6].bubbleSort());
    console.log("cat".substrings());
    
  5. substrings()
    String.prototype.substrings = function () {
      let letterArr = this.split("");
      let outArr = [];
      for (let i = 0; i < letterArr.length; i++) {
        for (let j = i + 1; j <= letterArr.length; j++){
          let subword = letterArr.slice(i,j).join("");
          if (!outArr.includes(subword)) {
            outArr.push(subword);
      return outArr;
    };
    console.log("cat".substrings());